C# 是否允许textbox仅支持数字和OemMinus?
在我的代码中,我使用这段代码来验证textbox是否仅支持,但我想允许OemMinus(-)也可以,我该怎么做C# 是否允许textbox仅支持数字和OemMinus?,c#,C#,在我的代码中,我使用这段代码来验证textbox是否仅支持,但我想允许OemMinus(-)也可以,我该怎么做 private void card_No_KeyDown(object sender, KeyEventArgs e) { nonNumberEntered = false; // Determine whether the keystroke is a number from the top of the keyboard. if (e.KeyCode &l
private void card_No_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = 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 &&
e.KeyCode == Keys.Oemplus)
{
// Determine whether the keystroke is a backspace.
if (e.KeyCode != Keys.Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = true;
}
}
}
}
private void card_No_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
MessageBox.Show("not allowed");
e.Handled = true;
}
}
private void card\u No\u KeyDown(对象发送方,KeyEventArgs e)
{
非numberrentered=false;
//确定击键是否为键盘顶部的数字。
如果(e.KeyCodeKeys.D9)
{
//确定击键是否为键盘上的数字。
如果(e.KeyCodeKeys.NumPad9&&
e、 KeyCode==Keys.Oemplus)
{
//确定击键是否为退格。
if(e.KeyCode!=Keys.Back)
{
//按下了非数字按键。
//将标志设置为true并在按键事件中计算。
非numberrentered=真;
}
}
}
}
私有无效卡\u否\u按键(对象发送器,按键事件参数e)
{
if(非numberEntered==true)
{
MessageBox.Show(“不允许”);
e、 已处理=正确;
}
}
您可以像这样处理按键事件
private void card_No_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsNumber(e.KeyChar) && e.KeyChar != '-' && e.KeyChar != (char)Keys.Back)
e.Handled = true;
}
但这不会阻止用户复制粘贴无效值,因此您也可以处理TextChanged
事件
private void card_No_TextChanged(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(card_No.Text))
{
if (Regex.Matches(card_No.Text, @"(-|\d)").Count != card_No.Text.Length)
{
//pasted an invalid value
MessageBox.Show("Invalid value entered");
}
}
}
你做错了。使用KeyPress事件而不是KeyDown。谢谢。。。但是它在(Regex)中给了我一个错误。。。我的代码无法识别它?我假设这是由于无法找到Regex
类而导致的错误。在代码顶部,使用System.Text.RegularExpressions添加代码>。谢谢你知道这是工作。。。。但退格也不起作用。。。。退格必须有效?啊,是的,你说得对。我已经更新了答案,所以退格应该有效(请参阅新的KeyPress
事件代码,它也允许Keys.Back
)。我删除了我的评论后,你很快就回复了我的评论,因为我意识到它全是狗屎。很抱歉。
private void card_No_KeyPress(object sender, KeyPressEventArgs e)
{
var textBox = sender as TextBox;
//handels integers, decimal numbers and OemMinus (-) character
if (((!char.IsControl(e.KeyChar))
&& (!char.IsDigit(e.KeyChar))
&& (e.KeyChar != '-')
) || (textBox != null
&& (e.KeyChar != '.'
&& (textBox.Text.IndexOf('.') > -1))))
e.Handled = true;
if (e.Handled)
MessageBox.Show(@"not allowed");
}
//prevent copy and past and delete pasted text
private void card_No_TextChanged(object sender, EventArgs e)
{
if (card_No.Text.Length >0)
{
if (Regex.Matches(card_No.Text, @"(-|\d)").Count != card_No.Text.Length)
{
if (!Clipboard.ContainsText()) return;
var txt = Clipboard.GetText();
if (card_No.Text.Contains(txt))
{
int ind = card_No.Text.IndexOf(txt, System.StringComparison.Ordinal);
var text = card_No.Text.Substring(0, ind);
card_No.Text = text;
MessageBox.Show(@"not allowed");
}
else if (txt.Contains(card_No.Text))
{
int ind = txt.IndexOf(card_No.Text, System.StringComparison.Ordinal);
var text = txt.Substring(0, ind);
card_No.Text = text;
MessageBox.Show(@"not allowed");
}
}
}
}