HI,Everyone GOOD NOON

I am creating a WINDOWS form using C# which have text box like part.no ( NUMBER ONLY NO Alphabets) i have used the following coding to accept number only .

private void Partno_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.Handled = (!char.IsDigit(e.KeyChar)) && (!char.IsControl(e.KeyChar));
        }

Now i need to display a text in a label(label1) like :"please enter the part no "if no value is given in the partno field. if number is entered then the label should be " ".

Please suggest whehter it's possible if so how r where can i find the solution

can anybody help me out

Yes just use string.IsNullOrEmpty(texBox.Text).

It returns true if the string is empty and false if it have charactors in it (including spaces).

So you could do:

label.Text = string.IsNullOrEmpty(textBox.Text) == true ? "please enter the part no " : " ";

OR

if (textBox.Text.Length > 0)
{
    label.Text = "please enter the part no ";
}
else
{
    label.Text = " ";
}
commented: 10 +0

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:

Func()
{
e.handled=(!char.IsDigit(e.KeyChar)) && (!char.IsControl(e.KeyChar));
if(e.handled==0)
{
Enter:
textbox1.text=e.keychar;
if (textBox1.Text == "")
{
   label1.text="please enter the part no";
 goto Enter;
}
else if (textBox1.Text!== "")
{
   label1.text=" ";
}

Thanks Guys its working :):) thanks for ur support :)

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)
        {

            if(string.IsNullOrEmpty(textBox1.Text)) 
            {
                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.
            }
        }

Milton

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.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.