如何阻止Ctrl-Enter上的ComboBox打开?

时间:2019-03-10 13:48:43

标签: c# winforms

Winforms C#。尝试使ComboBox上的Ctrl-Enter可以执行程序特定的内容,而不执行其他任何操作。

csv

当组合框具有焦点并按Ctrl键时,即使我已设置import csv list_info = [('1552150125', '02141592cc00000001', '2', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000200', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '5', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000500', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '6', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000600', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '7', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000700', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '8', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000800', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '9', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000900', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '10', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000a00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '11', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000b00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '12', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000c00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '13', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000d00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '14', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000e00', '0', '0', '0', '23'), ('1552150125', '02141592cc00000001', '15', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0000000f00', '0', '0', '0', '23')] h = 'time ID a b c d e f g h i j k l m n o p q r'.split() with open('file.csv', 'w',newline='') as myfile: wr = csv.writer(myfile, quoting=csv.QUOTE_NONE) wr.writerow(h) wr.writerows(list_info) ,也会弹出ComboBox下拉菜单以响应该键。我已经确认该问题与ComboBox AutoCompleteMode建议功能有关。如果此行被注释掉或更改为“追加”,它将停止执行此操作。但是我喜欢自动完成功能,如果可以的话,我希望保留它。

我已逐步调试,并验证了代码在KeyDown事件中通过e.handled = true之后但在KeyPress事件触发之前弹出该下拉菜单。

如何使ComboBox不响应Ctrl-Enter,除非我明确指示?

2 个答案:

答案 0 :(得分:2)

您可以通过覆盖ProcessCmdKey Method在派生的ComboBox中进行处理。

将此类定义添加到您的项目中并执行构建的操作。该控件将显示在“您的项目名称”“组件”部分顶部附近的工具箱中(假设VS配置为添加用户定义的控件)。此类公开了ControlEnterPressed事件,以便于对按键进行所需的操作。

public class CB : ComboBox
{
    public event Action<CB> ControlEnterPressed;

    protected virtual void OnControlEnterPressed()
    {
        ControlEnterPressed?.Invoke(this);
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        System.Diagnostics.Debug.Print(keyData.ToString());
        // runs before ProcessDialogKey, ProcessDialogKey is called if this method returns false
        bool ret = false;
        if ((keyData.HasFlag(Keys.Control) || keyData.HasFlag(Keys.ControlKey)) && (keyData.HasFlag(Keys.Return) || keyData.HasFlag(Keys.Enter)))
        {
            BeginInvoke(new Action(OnControlEnterPressed)); //let message processing finish before raising the event
            ret = true;   // indicate key handled
        }
        else
        {
            ret = base.ProcessCmdKey(ref msg, keyData);
        }
        return ret;
    }

}

答案 1 :(得分:1)

另一种方法是将下拉菜单的高度设置为1。

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
    ComboBox cb = new ComboBox();
    cb.DropDownStyle = ComboBoxStyle.DropDownList;
    cb.AutoCompleteSource = AutoCompleteSource.ListItems;
    cb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    cb.Location = new System.Drawing.Point(20, 50);
    cb.KeyDown += new System.Windows.Forms.KeyEventHandler(CBKeyDown);
    cb.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(CBPreviewKeyDown);
    cb.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CBKeyPress);
    this.Controls.Add(cb);

    List<string> someThings = new List<string>();
    someThings.Add("An item");
    someThings.Add("Another item");

    cb.DataSource = someThings;
}

private void CBPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Enter:
            if (e.Control)
            {
                e.IsInputKey = true;
            }

            break;
    }

    // Return back drop down height, in case ctrl + enter keys were pressed
    (sender as ComboBox).DropDownHeight = 100;
}

private void CBKeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Enter:
            if (e.Control)
            {
                // Do application specific stuff
                e.Handled = true;

                // Set drop down height to 1, in order to set height
                // that looks like drop down is not showed at all (only 1 pixel)
                (sender as ComboBox).DropDownHeight = 1;
            }

            break;
    }
}


private void CBKeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\n')
    {
        e.Handled = true;
    }
}
相关问题