tags:

views:

191

answers:

2

I have a windows form for a desktop app that has 7 fields,

how can I have the submit button disabled until the form validates?

I know I can validate the form when the user clicks the button, but if i have the button disabled what is the best way to call my validation method?

Using C# express 2008.

A: 

I don't know whether you have googled it but there are plenty of article going on the inter-web. Let me see :

http://www.codeproject.com/KB/miscctrl/validatingtextbox.aspx

http://msdn.microsoft.com/en-us/library/ms229603.aspx

http://msdn.microsoft.com/en-us/library/f6xht7x2.aspx

http://www.java2s.com/Code/CSharp/GUI-Windows-Form/SimpleFormValidation.htm

I hope they help.

Braveyard
hmm. kind of but it really only shows for one field, and not how to connect all fields validation to the enabling of the button.]
Alex
A: 

You can alwas call the validation method from the change event of all 7 controls. If you have bound the controls to some datasource the datasource shuld have an OnUpdated event.

private void TextBox1_Changed(object sender, EventArgs e)
{
 Validate();
}

private void ComboBox2_Changed(object sender, EventArgs e)
{
 Validate();
}

private void Validate()
{
 if(ValidationOk())
 {
  Button1.Enabled = true;
 }
 else
 {
  Button1.Enabled = false;
 }

}

Or maybe:

private void Validate()
{
 Button1.Enabled = ValidationOk();
}
magnus
Button1.Enabled = false; will be executed everytimee?
Shoban
How would you do it?
magnus
@ Thanks magnus , That is what I am working on right now,It's funny how peeps comment smack, but don't have there own answers.You do have to add an else though right/anyway now I have to get the validation method working properly.
Alex
@Shoban, I see your point now :) Just came out of the bed..
magnus
Thanks, simple validation method and if true = button enabled.
Alex