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

Recommended Answers

All 7 Replies

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;
      }

Here is an example of capturing the KeyDown and KeyPress events for a textbox to limit numeric input. You could also modify this logic to accept only the characters you want: Limiting input in control...

In addition to the examples sknake gave you, you might want to read up on Validating and Validated methods: Validating entered data...

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

i am using windows application not web froms

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..........:)

Mark this thread as solved, if it help you........

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.