如果您不介意使用 maskedTextBox,并且只是不喜欢下划线(正如您在评论中提到的那样),只需将 PromptChar 更改为空白即可。
您可以在 MaskedTextBox 属性的设计视图中执行此操作,也可以在如下代码中执行此操作:
myMaskedTextBox.PromptChar = ' ';
编辑:
或者,(如果您不想使用 maskedTextBox)您可以将 KeyDown 事件连接到 EventHandler,如下所示:
private void numericComboBox_KeyDown(object sender, KeyEventArgs e)
{
try
{
e.SuppressKeyPress = false;
// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace or arrow key
if ((e.KeyCode != Keys.Back) && (e.KeyCode != Keys.Up) && (e.KeyCode != Keys.Right) && (e.KeyCode != Keys.Down) && (e.KeyCode != Keys.Left))
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
e.SuppressKeyPress = true;
}
}
}
}
catch (Exception ex)
{
//Handle any exception here...
}
}