在Combobox中按Enter键清除文本

时间:2013-05-09 16:40:47

标签: c# combobox

我有一个包含项目列表的组合框。当用户开始输入时,应该打开下拉列表。点击密钥应该检查组合框的文本。

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

private void cmbItems_KeyDown(object sender, KeyEventArgs e)
{
    if (!e.KeyCode.Equals(Keys.Enter)) {
        if (!cmbItems.DroppedDown) {
            cmbItems.DroppedDown = true;
        }
    }
    else {
        //Check the text
    }
}

为什么组合框文本被清除并且有办法绕过它?

我的用户更喜欢我不使用AutoCompleteMode.SuggestAppend,但如果必须,我会这样做。

完整示例代码:

public class Demo : Form {
    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null)) {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        this.lblText = new System.Windows.Forms.Label();
        this.cmbItems = new System.Windows.Forms.ComboBox();
        this.SuspendLayout();
        // 
        // lblText
        // 
        this.lblText.AutoSize = true;
        this.lblText.Location = new System.Drawing.Point(152, 47);
        this.lblText.Name = "lblText";
        this.lblText.Size = new System.Drawing.Size(155, 13);
        this.lblText.TabIndex = 17;
        this.lblText.Text = "Text";
        // 
        // cmbItems
        // 
        this.cmbItems.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append;
        this.cmbItems.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
        this.cmbItems.Location = new System.Drawing.Point(12, 44);
        this.cmbItems.Name = "cmbItems";
        this.cmbItems.Size = new System.Drawing.Size(121, 21);
        this.cmbItems.TabIndex = 0;
        this.cmbItems.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cmbItems_KeyDown);
        // 
        // Demo
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(338, 78);
        this.Controls.Add(this.lblText);
        this.Controls.Add(this.cmbItems);
        this.Name = "Demo";
        this.Text = "Demo";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label lblText;
    private ComboBox cmbItems;

    DataTable _Items = new DataTable();

    public Demo()
    {
        InitializeComponent();

        BuildListOfItems();
    }

    private void cmbItems_KeyDown(object sender, KeyEventArgs e)
    {
        if (!e.KeyCode.Equals(Keys.Enter)) {
            if (!cmbItems.DroppedDown) {
                cmbItems.DroppedDown = true;
            }
        }
        else {
            //Check the text

        }

        lblText.Text = cmbItems.Text;
    }

    //Create a demo list of items.
    private void BuildListOfItems()
    {
        _Items.Columns.Add("Name").DataType = typeof(string);
        _Items.Columns.Add("ID").DataType = typeof(int);

        for (int i = (int)'a'; i < (int)'a' + 26; i++) {
            CreateItem(i, "", 0);
        }
        cmbItems.DataSource = _Items;
        cmbItems.ValueMember = "ID";
        cmbItems.DisplayMember = "Name";
    }

    private void CreateItem(int symbol, string prev, int count)
    {
        string newPrev = string.Format("{0}{1}", prev, (char)symbol);
        _Items.Rows.Add(newPrev, _Items.Rows.Count);
        if (count < 4)
            CreateItem(symbol + 1, newPrev, ++count);

    }
}

2 个答案:

答案 0 :(得分:0)

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

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++;
        }
    }

答案 1 :(得分:0)

这对我有用。

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