验证 WinForms 文本框(在 C# 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1048710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validating WinForms TextBox (in C#)
提问by
In TextBox_Leave event i need to check whether numbers entered in textbox is in serial number or not.If it is not in order then i need to display a message as "number" is missing
在 TextBox_Leave 事件中,我需要检查文本框中输入的数字是否在序列号中。如果不按顺序,那么我需要显示一条消息,因为缺少“数字”
For example :
例如 :
In textbox i have entered 3 and click tab : I need to display message as "Number is not in order , number "1" and "2" is missing "
在文本框中,我输入了 3 并单击选项卡:我需要将消息显示为“数字不正确,数字“1”和“2”丢失“
回答by Lennaert
I don't know whether this also works in c#2.0, this is my experience in c#3.0:
不知道在c#2.0中是否也可以,这是我在c#3.0中的经验:
Why do you use TextBox_Leave for that? The Validating-event should be used for validating whether input is correct.
为什么要为此使用 TextBox_Leave?Validating-event 应该用于验证输入是否正确。
Combine using the Validating-event with using an ErrorProvider (you can just drag it from the toolbox onto the form) to set an error message: it will be displayed as a (blinking) exclamation mark in a red triangle.
结合使用 Validating-event 和使用 ErrorProvider(您可以将它从工具箱拖到表单上)以设置错误消息:它将显示为红色三角形中的(闪烁)感叹号。
An ErrorProvider can also block any submit-actions.
ErrorProvider 还可以阻止任何提交操作。
回答by tzup
One trick is to retain focus in the textbox when trying to leave (with TAB for instance) in case of some condition (missing number):
一个技巧是在某些情况下(缺少数字)尝试离开(例如使用 TAB)时将焦点保留在文本框中:
private void textBox1_Leave(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text == "3")
tb.Focus();
}
Assuming you are using a standard textbox. You could also use third party controls that where you can cancel an event (e.Cancel = true) on some condition.
假设您使用的是标准文本框。您还可以使用第三方控件,您可以在某些条件下取消事件(e.Cancel = true)。
回答by THE DOCTOR
Try using a Masked TextBoxcontrol and set a custom property for this type of field validation.
尝试使用Masked TextBox控件并为此类字段验证设置自定义属性。
回答by Karthik
Alternatively you can also use Validating event of the text box.
或者,您也可以使用文本框的验证事件。
private void textBox1_Validating( object sender, CancelEventArgs e )
{
if ( textBox1.Text == "3" )
e.Cancel = true;
}
The text-box wont loose focus until it receives a valid input.
文本框在收到有效输入之前不会失去焦点。
回答by user3740175
I will show you how to validate Validating WinForms TextBox
(in C#).
我将向您展示如何验证Validating WinForms TextBox
(在 C# 中)。
Create a function:
public static void ChkBlankTextBoxes(object sender, string type) { if (sender is TextBox) { TextBox textbox = sender as TextBox; if (string.IsNullOrEmpty(textbox.Text)) { MessageBox.Show("Please enter correct value value.."); textbox.Focus(); } } }
Call to created function:
ChkBlankTextBoxes(txt_userID, textBoxtype); ChkBlankTextBoxes(txt_password, textBoxtype);
创建一个函数:
public static void ChkBlankTextBoxes(object sender, string type) { if (sender is TextBox) { TextBox textbox = sender as TextBox; if (string.IsNullOrEmpty(textbox.Text)) { MessageBox.Show("Please enter correct value value.."); textbox.Focus(); } } }
调用创建的函数:
ChkBlankTextBoxes(txt_userID, textBoxtype); ChkBlankTextBoxes(txt_password, textBoxtype);