c#关闭下拉菜单

本文关键字:下拉菜单 | 更新日期: 2023-09-27 18:18:41

按下Enter(键13)如何关闭下拉列表?

private void ComboBox_KeyPress(object sender, KeyPressEventArgs e)
{
        if (!Char.IsNumber(e.KeyChar) && e.KeyChar != ''b')
            e.Handled = true;
        if (e.KeyChar == 13)
            //close open dropdown list
}

c#关闭下拉菜单

要关闭任何组合框,请尝试

  DroppedDown = false;

在你的代码示例中,它可以是

private void ComboBox_KeyPress(object sender, KeyPressEventArgs e)
{
        if (!Char.IsNumber(e.KeyChar) && e.KeyChar != ''b')
            e.Handled = true;
        if (e.KeyChar == 13)
            (sender as ComboBox).DroppedDown = false;// <- close opened dropdown list
}

也许更好的方法是保留默认行为,让。net完成它的工作:

private void ComboBox_KeyPress(object sender, KeyPressEventArgs e) {
    if (e.KeyChar < ' ') // <- preserve default behavior for Enter, Esc etc.
      return;
    // Special conditions here
    if (!Char.IsNumber(e.KeyChar) && e.KeyChar != ''b')
      e.Handled = true;
}