#region Instance Methods ///<summary>
///Reset Font to default
///<summary>
public void ResetControlFont()
   {
   ControlFont=new Font ("Arial",9);
   }
public bool ShouldSerializeControlFont()
{return!ControlFont.Equals (new Font("Arial",9));}
/// <summary>
/// updata ListBox width based on measured string data
/// </summary>
private void updateListBoxWidth(string textString)
{
Graphic gr =lbxInput.CreateGraphics();
if(textString=="All")
{
for (int i=0;i<dv.Count;i++)
{
textSize=gr.MeasureString(dv[i]["ListValue"].ToString,ccbfont);
if ((int)textSize.Width>dropdownwidth)
{
dropdownwidth=(int)textSize.Width;
}
}
}
else
{
textSize=gr.MeasureString(textString,ccbfont);
if((int)textSize.Width>dropdownwidth){dropdownwidth=(int)textSize.Width;} }

lbxInput.Width =dropdownwidth+20;
this.Width=dropdownwidth+20;
gr.Dispose();
}
///<summary>
///Load ComboBox with data
///</summary>
private void CustomComboBox_Load(object sender,System.EventArgs e)
{
if (!this.DesignMode)
{
cnn=new System .Data .SqlClient.SqlConnection();
try
{
cnn.ConnectionString=@"data source=localhost;initial catalog="+ccbdatasource+@";integrated security=SSPI;persist security info=False;packet size=4096";
cnn.Open;
}
catch
{
try
{
cnn.ConnectionString=@"data source=localhost;initial catalog="+ccbdatasource+@";persist security info=False;"+"user id=sa;packet size=4096";
cnn.Open();
} catch (Exception ex)
{
MessageBox.Show(ex.Message);
ccbdatasource=null;
return;
} }
SqlCommand cmd=cnn.CreateCommand();
cmd.CommandText="SELECT ListValue FROM"+ccbdatatalble;
da=new SqlDataAdapter(cmd);
da.FillSchema(ds,SchemaType.Source);
da.Fill(ds,ccbdatatalble);
cnn.Close (); dv.Table=ds.Tables[ccbdatatable];
dv.Sort="ListValue ";
tbxInput.MaxLength=ds.Tables[0].Columns[0].MaxLength;
lbxInput.DataSource =dv;
lbxInput.DisplayMember="ListValue";
lbxInput.SelectedIndex=-1;

lbxInput.TabStop=false;
lbxInput.Visible=false;
dropdownwidth=ccbsize.X;
updateListBoxWidth("All"); }
}
///<summary>
///Process drop down button click
///</summary>
private void btnDropDownList_Click(object sender, System.EventArgs e)

if(lbxInput.Visible==false)
{
dv.RowFilter=null;
if(ccblistboxup==true)

this.Top -=ccbsize.Y-tbxInput.Height;
tbxInput.Top +=ccbsize.Y-tbxInput.Height;
btnDropDownList.Top +=ccbsize.Y-tbxInput.Height;
lbxInput.Top =0; }
this.Height=ccbsize.Y;
this.Width=dropdownwidth+20;
lbxInput.Visible =ture;
} else {
lbxInput.Visible =false;
this.Width=ccbsize.X+btnDropDownList.Width;
this.Height=tbxInput.Height;
if(ccblistboxup==ture)
{
this.Top+=ccbsize.Y-tbxInput.Height;
btnDropDownList.Top=2;
tbxInput.Top=0;
lbxInput.Top=tbxInput.Height;

}
}
tbxInput.Focus( );
}
///<summary>
///UpdateListBox based on text changes In TextBox
///</summary>
private void tbxInput_TextChanged(object sender,System.EventArgs e)
{
dv.RowFilter ="ListValue like'"+tbxInput.Text+"%'";
lbxInput.SelectedIndex=-1;
if(tbxInput.Focused==false||dv.Count <1||tbxInput.Text .Length <1)
{
if (lbxInput.Visible ==ture)
{
lbxInput.Visible ==false;
this.Width =ccbsize.X +btnDropDownList.Width ;
this.Height =tbxInput.Height ;
if(ccblistbox==ture)
{
this.Top +=ccbsize.Y -tbxInput.Height ;
tbxInput.Top =0;
btnDropDownList.Top =2;
lbxInput.Top =tbxInput.Height ;
}
}
}
else
{
if(lbxInput.Visible ==false)
{
if(ccblistboxup==ture)
 {
 this.Top -=ccbsize.Y -tbxInput.Height ;
 tbxInput.Top +=ccbsize.Y -tbxInput.Height ;
btnDropDownList.Top +=ccbsize.Y -tbxInput.Height ;  lbxInput.Top =0;}
this.Height =ccbsize.Y ;
this.Width =dropdownwidth+20;
lbxInput.Visible =ture;
lbxInput.SelectedIndex=-1;
}
}
}

解决方案 »

  1.   

    private void tbxInput_KeyDown(object sender,System.Windows .Forms .KeyEventArgs e)
    {
    if(e.KeyCode ==Keys.Down )
    {
    e.Handled =ture;
    if(lbxInput.Visible ==false)
    {
    dv.RowFilter ="ListValue like'"+tbxInput.Text +"%'";
    if(dv.Count >0)
    {
    if(ccblistboxup==ture)
    {
    this.Top -=ccbsize.Y -tbxInput.Height ;
    tbxInput.Top+=ccbsize.Y -tbxInput.Height ;
    btnDropDownList.Top +=ccbsize.Y -tbxInput.Height ;
    lbxInput.Top =0;
    }
    this.Height =ccbsize.Y ;
    this.Width =dropdownwidth+20;
    lbxInput.Visible =ture; }
    }
    else
    {
    if(lbxInput.SelectedIndex +1<lbxInput.Items .Count )
    {lbxInput.SelectedIndex =lbxInput.SelectedIndex +1;}
    }
    if(e.KeyCode ==Keys.Up )
    {
    e.Handled =ture;
    if(lbxInput.Visible ==ture)
    {
    if(lbxInput.SelectedIndex >0)
    {
    lbxInput.SelectedIndex =lbxInput.SelectedIndex -1;
    }
    }
    }
    if(e.KeyCode ==Keys.Delete )
    {
    if(lbxInput.SelectedIndex ==-1)
    {
    MessageBox.Show ("Please use the down arrow to select an item from"+
    "the list to delete.","Delete Selection",
    MessageBoxButtons.OK,MessageBoxIcon.Warning );}
    else if (lbxInput.Visible ==ture)
    {
    if(MessageBox.Show ("Delete"+lbxInput.Text +"?",
    "Delete Selection",MessageBoxButtons.YesNo,MessageBoxIcon.Warning )==DialogResult.Yes)
    {
    deleteRow();
    this.Height =tbxInput.Height ;
    lbxInput.Visible =false;
    this.Width =ccbsize.X +btnDropDownList.Width ;
    if(ccblistboxup==ture)
    {
    this.Top +=ccbsize.Y -tbxInput.Height ;
    tbxInput.Top =0;
    btnDropDownList.Top =2;
    lbxInput.Top =tbxInput.Height ;}
    }
    }
    }
    }
    }
    ///<summary>
    ///Insert entry into database
    ///</summary>
    private void insertRow()
    {
    SqlCommand cmd=cnn.CreateCommand ();
    cmd.CommandText ="INSERT INTO"+ccbdatatable+"(ListValue)VALUES(@ListValue)";
    cmd.Parameters .Add (new SqlParameter ("@ListValue",SqlDbType.VarChar ,tbxInput.MaxLength ));
    cmd.Parameters ["@ListValue"].Value =tbxInput.Text ;
    cmd.CommandType =System.Data. CommandType.Text ;
    cnn.Open ();
    cmd.ExecuteNonQuery ();
    cnn.Close ();
    DataRow NewDataRow=ds.Tables [ccbdatatalble].Rows.Add(NewDataRow);
    ds.AcceptChanges ();}
    ///<summary>
    ///Delete entry from database
    ///</summary>
    private void deleteRow()
    {
    SqlCommand cmd=cnn.CreateCommand ();
    cmd.CommandText="DELETE FROM"+ccbdatatable+"WHERE ListValue=@ListValue";
    cmd.Parameters .Add (new SqlParameter ("@ListValue",SqlDbType.VarChar,tbxInput.MaxLength ));
    cmd.Parameters ["@ListValue"].Value=tbxInput.Text ;
    cmd.CommandType =System.Data .CommandType .Text ;
    cnn.Open ();
    cmd.ExecuteNonQuery ();
    cnn.Close ();
    DataRow row;
    DataRow [] rows;
    rows=ds.Tables [ccbdatatable].Select ("ListValue='"+lbxInput.Text +"'");
    row=rows[0];
    row.Delete ();
    ds.AcceptChanges (); }
    ///<summary>
    ///Process mouse clicks in ListBox
    ///Left mouse click selects item
    ///Right mouse click offers detele option for selected item
    ///</summary>
    private void lbxInput_MouseUp(object sender,System.Windows .Forms .MouseEventArgs e)
    {
    if(e.Button ==MouseButtons.Right )
    {
    lbxInput.SelectedIndex =(lbxInput.TopIndex +(e.Y /lbxInput.ItemHeight ));
    if(MessageBox.Show ("Delete"+lbxInput.Text +"?","Delete Selection",
    MessageBoxButtons.YesNo ,MessageBoxIcon.Warning )==DialogResult.Yes )
    {
    deleteRow();
    this.Height =tbxInput.Height ;
    lbxInput.Visible =false;
    this.Width =ccbsize.X +btnDropDownList.Width ;
    if(ccblistboxup==ture)
    {
    this.Top +=ccbsize.Y-tbxInput.Height ;
    tbxInput.Top =0;
    btnDropDownList.Top =2;
    lbxInput.Top =tbxInput.Height ;
    }
    }
    }
    else
    {
    if(tbxInput.Text==lbxInput.Text )
    {
    this.Height =tbxInput.Height ;
    lbxInput.Visible =false;
    this.Width =ccbsize.X +btnDropDownList.Width ;
    if(ccblistboxup==ture)
    {
    this.Top +=ccbsize.Y -tbxInput.Height ;
    tbxInput.Top =0;
    btnDropDownList.Top =2;
    lbxInput.Top =tbxInput.Height ;}
    }
    tbxInput.Text =lbxInput.Text ;
    }
    }
    #endregion
      

  2.   

    // /// <summary> 
    // /// 必需的设计器变量。
    // /// </summary>
    // private System.ComponentModel.Container components = null; #region Method Overrides
    ///<summary>
    ///Process ComboBox tab and enter keys
    ///</summary>
    protected override bool ProcessDialogKey(Keys keyData)
    {
    if(KeyData==Keys.Tab ||Keys.Enter)
    {
    if(lbxInput.Visible ==ture)
    {
    if(lbxInput.SelectedIndex ==-1)
    {ccbtext=tbxInput.Text ;}
    else
    {
    tbxInput.Text =lbxInput.Text ;
    ccbtext=lbxInput.Text ;}
    lbxInput.Visible =false;
    this.Width =ccbsize.X+btnDropDownList.Width ;
    this.Height =tbxInput.Height ;
    if(ccblistboxup==ture)
    {
    this.Top +=ccbsize.Y -tbxInput.Height ;
    tbxInput.Top =0;
    btnDropDownList.Top =2;
    lbxInput.Top =tbxInput.Height ;}
    }
    dv.RowFilter ="ListValue like'"+tbxInput.Text +"'";
    if(dv.Count <1&&tbxInput.Text .Length >0)
    {insertRow();}
    base.ProcessDialogKey(KeyData);
    updateListBoxWidth(tbxInput.Text );
    return true;
    }
    if(keyData!=Keys.Delete )
    {lbxInput.SelectedIndex =-1;}
    base.ProcessDialogKey(keyData);
    return false;
    }
    #endregion 



    #region Internal Classes
    internal class  CustomComboBoxDesigner: ControlDesigner
    {
    # region Method Overrides
    protected override void PostFilterProperties(System.Collections.IDictionary properties)
    {
    properties.Remove("Text");
    properties.Remove("DataBindings");
    properties.Remove("DynamicPropertise");
    properties.Remove("AccessibleDescription");
    properties.Remove("AccessibleName");
    properties.Remove("AccessibleRole");
    properties.Remove("AllowDrop");
    properties.Remove("AutoScroll");
    properties.Remove("AutoScrollMargin");
    properties.Remove("AutoScrollMinSize");
    properties.Remove("BackColor");
    properties.Remove("BackgroundImage");
    properties.Remove("Dock");
    properties.Remove("DockPadding");
    properties.Remove("ForeColor");
    properties.Remove("Font");
    properties.Remove("ImeMode");
    properties.Remove("Size");
    }
    #endregion
    }
    #endregion
    //
    // /// <summary> 
    // /// 清理所有正在使用的资源。
    // /// </summary>
    // protected override void Dispose( bool disposing )
    // {
    // if( disposing )
    // {
    // if(components != null)
    // {
    // components.Dispose();
    // }
    // }
    // base.Dispose( disposing );
    // }
    //
    //

    }
    }
      

  3.   

    D:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\CustomControls\CustomComboBox.cs(254): 修饰符不能放置在属性或事件访问器声明中
    D:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\CustomControls\CustomComboBox.cs(254): 应为 get 或 set 访问器
      

  4.   

    那你的CustomComboBox.cs(254)的代码是什么?
      

  5.   

    public void ResetControlFont()
       {
       ControlFont=new Font ("Arial",9);
       }