you can set validation in GUI mode like if you want only numbers to be entered then you can enter numbers which will be only accepted that text box in text box's properties.
or you can do it with the code like:
e.handled=(!char.IsDigit(e.KeyChar)) && (!char.IsControl(e.KeyChar));
if (textBox1.Text == "")
label1.text="please enter the part no";
else if (textBox1.Text!== "")
Knight YUVA i would not recomend iether solutions to your post.
Both contain bad programming practices.
A better way is to use the the textboxs' validating event which is fired as the the textbox looses focus.
See sample code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
e.Handled = (!char.IsDigit(e.KeyChar)) && (!char.IsControl(e.KeyChar));
private void textBox1_Validating(object sender, CancelEventArgs e)
MessageBox.Show("This field can not be empty.", "Input Error", MessageBoxButtons.OK); // show input error message.
textBox1.Focus(); // set focus back to the offending text box.
My suggestions didn't specify what method to put the validation in but only the validation. They are two different and clean ways to do the validation (which is what was asked), with the first being the best of the two.
Unnecessary clicks are bad practice.
Knight YUVA probably specified using a label for a reason. Highlighting an individual field using it's label, on a form full of fields, would be better than having a massage box for every field.
Saying that yes you're right about when the validation should be carried out (which helps me with a problem of my own).
If it were me I would use error provider controls, containing relevant error messages, which are shown, or not shown, as a result of the validation carried out within the textBox1_Validating() method.
I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:
Runtime run = Runtime.getRuntime();
Process pr = ...
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.