Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am working with errorin windows form. two textbox and a submit button. the textbox are all required field must be submitted
the problem is when i click on submit without filling the textbox i should get an error that this all field are compulsory how can i do that?
by clicking on button error comes but after clicking the message box the user should get which textbox should be fille i used tabindex but only the fisrt textbox is showing error and i want both of them should be errored
Posted
Updated 16-Dec-12 18:03pm
v3

Hi Friend ! Just try this code !!! This will sure solve your problem.
C#
private void button1_Click(object sender, EventArgs e)
       {
           if (textBox1.Text == "")
           {
               errorProvider1.SetError(textBox1, "Please Enter the value in First Box.");
               MessageBox.Show("Data not Submitted", "Form", MessageBoxButtons.OK, MessageBoxIcon.Error);
               textBox1.Focus();
           }
           else if (textBox2.Text == "")
           {
               errorProvider1.SetError(textBox2, "Please Enter the value in Second Box.");
               MessageBox.Show("Data not Submitted", "Form", MessageBoxButtons.OK, MessageBoxIcon.Error);
               textBox2.Focus();
           }
           else
           {
               MessageBox.Show("Data Submitted", "Form", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
       }

       private void textBox1_TextChanged(object sender, EventArgs e)
       {
           if (textBox1.Text != "")
           {
               errorProvider1.SetError(textBox1, "");
           }
       }

       private void textBox2_TextChanged(object sender, EventArgs e)
       {
           if (textBox2.Text != "")
           {
               errorProvider1.SetError(textBox2, "");
           }
       }


If any problem, then plz reply or comment.
 
Share this answer
 
Comments
sariqkhan 10-Nov-12 13:09pm    
awsome code. but the problem is.
second error provider is not showing the error when the button is clicked only the first error provider is throwing error
Ank_ush 10-Nov-12 13:35pm    
I have used only one error provider, if you use two error providers then also it's ok.
But my suggestion is Use only one error provider, Make your program as easy as possible and it will not be ambiguous also. If you want to use 2 error providers then use it.
sariqkhan 13-Nov-12 1:19am    
bro i means the second textbox's is not showing the error when i clicked without filling up the form. only first textbox is showing error not the second textbox
wojjas369 13-Nov-12 4:58am    
Nice, yes, of course, the TextChanged can be used instead. And I like the redirect of focus with textBox2.Focus(); I also thought of suggesting this but not knowing where to focus if both are empty I left it out.
sariqkhan 13-Nov-12 8:55am    
so what editing i have to do? for diplaying error provider on both the textbox when submit button is clicked without filling both the textbox? what i have to edit?
1)
C#
return string.IsNullOrEmpty(NameToValidate) ? false : true;

Is equivalent to:
C#
if (NameToValidate == "")
{
    return false;
}
else
{
    return true;
}

It's just a shorter form of writing if-else statements that are so common.
But there is one more difference. It's the IsNullOrEmpty() method of the string class. If null is sent to isNameValid() and you are using NameToValidate == "" your function will crash cause you can't compare null (nothing) with an empty string "". That's why it's safer to use IsNullOrEmpty() this method returns true/false and thus can be used directly in the if-test. Just as we use iNameValid directly in an if-statement.

2)
C#
ValidatingTextBox((TextBox)sender);

From within this event-handler the private method ValidatingTextBox is called. As the parameter sender is specified. But, the private variable sender is of the general type Object which can be converted to the type that ValidateingTextBox expects. ValidatingTextBox expects System.Windows.Forms.TextBox and that's why an type convert is happening. The type convert is done by using syntax: (new-type-to-convert-to)object-to-convert. in this case: (TextBox)sender
 
Share this answer
 
Comments
sariqkhan 13-Nov-12 9:07am    
awsome explanation bro.
+5. you should be a teacher
: )
and i have some doubt
public bool isNameValid(string textBox) { return string.IsNullOrEmpty(textBox) ? false : true; }
private bool ValidatingTextBox(TextBox textBox)
{ if (isNameValid(textBox.Text)) { errorProvider1.SetError(textBox, "");
return true;
}
else { errorProvider1.SetError(textBox, "enter something"); return false; } }

here the first function is isNameValid. we have passed the textbox string to this. right said???
but what output is given out? true or false if null or empty is filled so true will be returned or false will be return?
sariqkhan 13-Nov-12 9:12am    
and about second question i got your 2nd conversion it was also an awsome explanation but. you have explained about conversion and cleared my funda. but here what is the sender??? ValidatingTextBox(sender); is the main and we have converted object into textbox which becomes ValidatingTextBox((TextBox)sender);
what is that
"ValidatingTextBox(sender);" i didint heard about sender before?
wojjas369 13-Nov-12 11:27am    
Thank you. I'm glad you understood.
>> "here the first function is isNameValid. we have passed the textbox string to this. right said???"
Yes, the Text property of TextBox returns a String.
>> "but what output is given out?"
As expected, if string i valid true is returned, else false. Just try it out with the debugger.
Hi,

Note that the Validating event fires when the textbox looses focus. So, if I just open the form and klick the button only the button's Click-event will fire and not the Validating events.

The trick is to extract the logic from within the Validating event-handler into a seperate function. In my code example I called this function ValidatingTextBox().

This allows us to call the extracted logic from several handlers. From the Validating and from the Click event-handler for the button.

In the example below I did some refactoring, the Validating was doing the same thing. Hope it's still clear and shows the point.


C#
#region Methods
public Form1()
{
    InitializeComponent();
}

public bool isNameValid(string NameToValidate)
{
    return string.IsNullOrEmpty(NameToValidate) ? false : true;
}

private bool ValidatingTextBox(TextBox TextBoxToValidate)
{
    if (isNameValid(TextBoxToValidate.Text))
    {
        errorProvider1.SetError(TextBoxToValidate, "");
        return true;
    }
    else
    {
        errorProvider1.SetError(TextBoxToValidate, "enter something");
        return false;
    }
}
#endregion

#region Event Handlers
private void textBox_Validating(object sender, CancelEventArgs e)
{
    ValidatingTextBox((TextBox)sender);
}
private void button1_Click(object sender, EventArgs e)
{
    bool vName = ValidatingTextBox(textBox1);
    bool vName2 = ValidatingTextBox(textBox2);

    if (vName && vName2)
    {
        MessageBox.Show("sucess");
    }
    else
    {
        MessageBox.Show("error");
    }
}
#endregion
 
Share this answer
 
Comments
sariqkhan 10-Nov-12 9:12am    
i have written this.

public bool isNameValid(string textBox)
{
return string.IsNullOrEmpty(textBox) ? false : true;
}

private bool ValidatingTextBox(TextBox textBox)
{
if (isNameValid(textBox.Text))
{
errorProvider1.SetError(textBox, "");
return true;
}
else
{
errorProvider1.SetError(textBox, "enter something");
return false;
}
}




private void button1_Click(object sender, EventArgs e)
{
bool vName = ValidatingTextBox(textBox1);
bool vName2 = ValidatingTextBox(textBox2);

if (vName && vName2)
{
MessageBox.Show("sucess");
}
else
{
MessageBox.Show("error");
}
}

private void textBox1_Validating(object sender, CancelEventArgs e)
{
ValidatingTextBox((TextBox)sender);
}

private void textBox2_Validating(object sender, CancelEventArgs e)
{
ValidatingTextBox((TextBox)sender);
}
sorry i am a beginer. i dont know much about coding
can you suggest what you have done? i didint understand the logic part.
can you modify the code with comment? plz sir it was awsome code. but can you do commenting then i will understand what you have done.
i am a newcommer to .net
Abhishek Pant 10-Nov-12 9:50am    
the above code is good if you are using validation as a tool component property.you can do the same simply by using if else int it if it any one of textbox remains blank or not correct you can use error provider to show that error
sariqkhan 10-Nov-12 12:57pm    
ohkay. thats good
wojjas369 12-Nov-12 17:07pm    
Hi, does "okhay" mean that your problem is solved?
If not, please specify more precisely what "logic part" you did not understand and I will be glad to try to explain. Maybe you find the type conversion confusing "(TextBox)sender"? Don't worry that you are a beginner we have all been once ;-)
sariqkhan 13-Nov-12 1:16am    
what does this mean

1. return string.IsNullOrEmpty(NameToValidate) ? false : true;
2. ValidatingTextBox((TextBox)sender);
this two are confusing what are this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900