要关闭任何组合框,请尝试
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;
}