我有一个windows窗体应用程序与一个文本框控件,我想只接受整数值。在过去,我通过重载KeyPress事件并删除不符合规范的字符来进行这种验证。我已经看了MaskedTextBox控件,但我想一个更通用的解决方案,可以与也许正则表达式,或依赖于其他控件的值。
理想情况下,按下非数字字符要么不产生结果,要么立即向用户提供关于无效字符的反馈。
我有一个windows窗体应用程序与一个文本框控件,我想只接受整数值。在过去,我通过重载KeyPress事件并删除不符合规范的字符来进行这种验证。我已经看了MaskedTextBox控件,但我想一个更通用的解决方案,可以与也许正则表达式,或依赖于其他控件的值。
理想情况下,按下非数字字符要么不产生结果,要么立即向用户提供关于无效字符的反馈。
当前回答
这正是设计已验证/正在验证事件的目的。
下面是MSDN上关于这个主题的文章:http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx
TL;DR版本:检查validate事件中的. text属性,并在数据无效时设置e.Cancel=True。
当你设置e.Cancel=True时,用户不能离开这个字段,但是你需要给他们一些错误的反馈。我将方框的背景颜色更改为浅红色,以表示有问题。确保将其设置回SystemColors。窗口时调用一个良好的值。
其他回答
在按钮点击,你可以检查文本框的文本循环:
char[] c = txtGetCustomerId.Text.ToCharArray();
bool IsDigi = true;
for (int i = 0; i < c.Length; i++)
{
if (c[i] < '0' || c[i] > '9')
{ IsDigi = false; }
}
if (IsDigi)
{
// do something
}
故障安全和简单的“递归”方法,可用于多个文本框。
它可以阻止错误的键盘输入字符,也可以粘贴值等。它只接受整数,最大数字长度是字符串类型的最大长度(它是int,真的很长!)
public void Check_If_Int_On_TextChanged(object sender, EventArgs e)
{
// This method checks that each inputed character is a number. Any non-numeric
// characters are removed from the text
TextBox textbox = (TextBox)sender;
// If the text is empty, return
if (textbox.Text.Length == 0) { return; }
// Check the new Text value if it's only numbers
byte parsedValue;
if (!byte.TryParse(textbox.Text[(textbox.Text.Length - 1)].ToString(), out parsedValue))
{
// Remove the last character as it wasn't a number
textbox.Text = textbox.Text.Remove((textbox.Text.Length - 1));
// Move the cursor to the end of text
textbox.SelectionStart = textbox.Text.Length;
}
}
只是因为在一条线上做事情总是更有趣……
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
注意:这并不阻止用户复制/粘贴到此文本框。这并不是清除数据的安全方法。
只需使用NumericUpDown控件,并设置那些丑陋的上下按钮可见性为假。
numericUpDown1.Controls[0].Visible = false;
NumericUpDown实际上是一个包含“旋转框”(上下按钮)的控件集合,一个文本框和一些代码来验证和wange-jangle。
标记:
YourNumericUpDown.Controls[0].visible = false
将隐藏按钮,同时保持底层代码活动。
虽然不是一个明显的解决方案,但它简单而有效。. controls[1]将隐藏文本框部分,如果你想这样做的话。
这个功能包括复制粘贴,拖放,按下键,防止溢出,非常简单
public partial class IntegerBox : TextBox
{
public IntegerBox()
{
InitializeComponent();
this.Text = 0.ToString();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
private String originalValue = 0.ToString();
private void Integerbox_KeyPress(object sender, KeyPressEventArgs e)
{
originalValue = this.Text;
}
private void Integerbox_TextChanged(object sender, EventArgs e)
{
try
{
if(String.IsNullOrWhiteSpace(this.Text))
{
this.Text = 0.ToString();
}
this.Text = Convert.ToInt64(this.Text.Trim()).ToString();
}
catch (System.OverflowException)
{
MessageBox.Show("Value entered is to large max value: " + Int64.MaxValue.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Text = originalValue;
}
catch (System.FormatException)
{
this.Text = originalValue;
}
catch (System.Exception ex)
{
this.Text = originalValue;
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK , MessageBoxIcon.Error);
}
}
}