C# 在组合框中按Enter键可清除文本

C# 在组合框中按Enter键可清除文本,c#,combobox,C#,Combobox,我有一个带有项目列表的组合框。当用户开始键入时,应打开下拉列表。按此键应检查组合框的文本 为了实现这一点,我在keydown事件中设置了DroppedDown=true。但是,在我这样做之后,击键会导致组合框的文本为空 private void cmbItems\u KeyDown(对象发送方,KeyEventArgs e) { 如果(!e.KeyCode.Equals(Keys.Enter)){ 如果(!cmbItems.DroppedDown){ cmbItems.DroppedDown=t

我有一个带有项目列表的组合框。当用户开始键入时,应打开下拉列表。按此键应检查组合框的文本

为了实现这一点,我在keydown事件中设置了DroppedDown=true。但是,在我这样做之后,击键会导致组合框的文本为空

private void cmbItems\u KeyDown(对象发送方,KeyEventArgs e)
{
如果(!e.KeyCode.Equals(Keys.Enter)){
如果(!cmbItems.DroppedDown){
cmbItems.DroppedDown=true;
}
}
否则{
//检查文本
}
}
为什么组合框文本被清除,有没有办法绕过它

我的用户希望我不要使用AutoCompleteMode.SuggestAppend,但如果必须的话,我会这样做

完整示例代码:

公共类演示:表单{
private System.ComponentModel.IContainer components=null;
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null)){
组件。Dispose();
}
基地。处置(处置);
}
私有void InitializeComponent()
{
this.lblText=new System.Windows.Forms.Label();
this.cmbItems=new System.Windows.Forms.ComboBox();
这个.SuspendLayout();
// 
//lblText
// 
this.lblText.AutoSize=true;
this.lblText.Location=新系统图纸点(152,47);
this.lblText.Name=“lblText”;
this.lblText.Size=新系统图纸尺寸(155,13);
this.lblText.TabIndex=17;
this.lblText.Text=“Text”;
// 
//CMB项目
// 
this.cmbItems.AutoCompleteMode=System.Windows.Forms.AutoCompleteMode.Append;
this.cmbItems.AutoCompleteSource=System.Windows.Forms.AutoCompleteSource.ListItems;
this.cmbItems.Location=新系统图纸点(12,44);
this.cmbItems.Name=“cmbItems”;
this.cmbItems.Size=新系统图纸尺寸(121,21);
this.cmbItems.TabIndex=0;
this.cmbItems.KeyDown+=new System.Windows.Forms.KeyEventHandler(this.cmbItems\u KeyDown);
// 
//演示
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统图尺寸(338,78);
this.Controls.Add(this.lblText);
this.Controls.Add(this.cmbbitems);
this.Name=“Demo”;
this.Text=“Demo”;
此选项为.resume布局(false);
这个。执行布局();
}
#端区
private System.Windows.Forms.Label lblText;
私人组合框项目;
DataTable_Items=新DataTable();
公共演示()
{
初始化组件();
BuildListOfItems();
}
私有无效CMBU项\u键关闭(对象发送方,KeyEventArgs e)
{
如果(!e.KeyCode.Equals(Keys.Enter)){
如果(!cmbItems.DroppedDown){
cmbItems.DroppedDown=true;
}
}
否则{
//检查文本
}
lblText.Text=cmbItems.Text;
}
//创建项目的演示列表。
私有void buildListoItems()
{
_Items.Columns.Add(“Name”).DataType=typeof(字符串);
_Items.Columns.Add(“ID”).DataType=typeof(int);
对于(int i=(int)'a';i<(int)'a'+26;i++){
CreateItem(i,“,0);
}
cmbItems.DataSource=\u项;
cmbItems.ValueMember=“ID”;
cmbItems.DisplayMember=“名称”;
}
私有void CreateItem(整数符号、字符串前缀、整数计数)
{
字符串newPrev=string.Format(“{0}{1}”,prev,(char)符号);
_Items.Rows.Add(newPrev,_Items.Rows.Count);
如果(计数<4)
CreateItem(符号+1,newPrev,++count);
}
}

我在大学早期做了一些这样的事情,然后回去检查我的代码。我不是100%确定,但这就是你想要的吗

private void comboCode_TextType(object sender, EventArgs e)
    {
        int itemsIndex = 0;
        foreach (string item in cmbGageCode.Items)
        {
            if (item.IndexOf(cmbGageCode.Text) == 0)
            {
                cmbGageCode.SelectedIndex = itemsIndex;
                cmbGageCode.Select(cmbGageCode.Text.Length - 1, 0);
                break;
            }
            itemsIndex++;
        }
    }
这对我有用

string perfilText = "";
...   
private void cbPerfis_KeyUp(object sender, KeyEventArgs e)
{
            if (e.KeyData != Keys.Enter)
            {                
                perfilText = cbPerfis.Text;
            }
            else
            {
                cbPerfis.Text = perfilText;
                cbOrdens.Focus();
            }
}
string perfilText = "";
...   
private void cbPerfis_KeyUp(object sender, KeyEventArgs e)
{
            if (e.KeyData != Keys.Enter)
            {                
                perfilText = cbPerfis.Text;
            }
            else
            {
                cbPerfis.Text = perfilText;
                cbOrdens.Focus();
            }
}