0

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
}
4

1 回答 1

4

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

  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;
}
于 2013-06-16T14:06:29.947 回答