hi,
i want to write code on validation of text box , combo box
for make them not null fields,
And other for mae a text box only accept integer , one only accept string not integer value.
so pls. tell me where shud i write dis code & give me some examples of code

if u want to know z fields r empty or not ...
use their name like this

if(txtbox1.Text==String.Emtpy) { //do something } and if u want to only accept integers or strings .... 1st go to keystroke property ..... then check wheterh the character key pressed is string or int and like that[CODE=C#] if(txtbox1.Text==String.Emtpy)
{
//do something
}
and if u want to only accept integers or strings ....
1st go to keystroke property ..... then check wheterh the character key pressed is string or int and like that

Try something like this:

private void btnAdd_Click(object sender, EventArgs e)
    {
      //Must be an integer
      int scoreInput;
      if (string.IsNullOrEmpty(txtScore.Text) || !int.TryParse(txtScore.Text, out scoreInput))
      {
        txtScore.Focus();
        MessageBox.Show("Please enter a valid score", "Input Error");
        txtScore.Focus(); //in case they double click the message and select another ctrl
        return;
      }
      else if ((scoreInput < 0) || (scoreInput > 100))
      {
        txtScore.Focus();
        MessageBox.Show("The score must be between 0 and 100", "Input Error");
        txtScore.Focus(); //in case they double click the message and select another ctrl
        return;
      }

      int avgInput;
      if (string.IsNullOrEmpty(txtAverage.Text) || !int.TryParse(txtAverage.Text, out avgInput))
      {
        txtAverage.Focus();
        MessageBox.Show("Average is not allowed to be a number. Please provide a different value", "Input Error");
        txtAverage.Focus(); //in case they double click the message and select another ctrl
        return;
      }

Are these for a web page? You can look at RegularExpressionValidator and RequiredFieldValidator too

i am using windows application not web froms

Well in win forms you can do simple like this on button click:

if(textbox1.text=" ")
{
Messagebox.show("Please enter some value");
}

Similarly for combobox...........
Hope this help you..........:)

This article has been dead for over six months. Start a new discussion instead.