A simple Form OK button question
-
Hi, In my WinForm App, I have to show another form2. but I do have to know if the user click on OK or Cancel. therefore I assigned DialogResult property of OK and Cancel button on Form2 to OK and Cancel, respectively. It works fine and I can use Dialogresult to figure out whether the OK or Cancel button is clicked to close the Form2. The problem comes in when I add a errorProvider1 to form2, I use it to make sure a couple of textBoxes are not empty and then SetError on ErrorProvider. Because the OK button has DialogResult.OK property set, the dialog closes before I got chance to view the error. Is there anyway to prevent the dialog form close if I have any errors. It is so easy to do in old MFC days. :-(. Dion
-
Hi, In my WinForm App, I have to show another form2. but I do have to know if the user click on OK or Cancel. therefore I assigned DialogResult property of OK and Cancel button on Form2 to OK and Cancel, respectively. It works fine and I can use Dialogresult to figure out whether the OK or Cancel button is clicked to close the Form2. The problem comes in when I add a errorProvider1 to form2, I use it to make sure a couple of textBoxes are not empty and then SetError on ErrorProvider. Because the OK button has DialogResult.OK property set, the dialog closes before I got chance to view the error. Is there anyway to prevent the dialog form close if I have any errors. It is so easy to do in old MFC days. :-(. Dion
Why not just catch the yourbutton.OnClick event? private void button1_Click(object sender, System.EventArgs e) { if(textBox1.Text.Length == 0 || textBox2.Text.Length == 0)//etc. { //Your error handling code MessageBox.Show("Please, enter some text"); return; } this.Close(); } Hope being helpful Cheers, Gogou GAtanasov
-
Why not just catch the yourbutton.OnClick event? private void button1_Click(object sender, System.EventArgs e) { if(textBox1.Text.Length == 0 || textBox2.Text.Length == 0)//etc. { //Your error handling code MessageBox.Show("Please, enter some text"); return; } this.Close(); } Hope being helpful Cheers, Gogou GAtanasov
Thanks for your help. I definitely can do my validation on btnOK_click event, but how do I know from my main form whether an user clicked on OK button or Cancel button if I do not set the DialogResult property. But if I set DialogResult property, the OK button closes the form before I can see the validation errors. I'd like to achieve BOTH: 1. TextBox Validation when OK is clicked, if there is error, do not close the form. 2. If there is no error, the Form2 is closed. From the caller, I want to know whether an user click on OK or Cancel. How do I achieve this? Dion
-
Thanks for your help. I definitely can do my validation on btnOK_click event, but how do I know from my main form whether an user clicked on OK button or Cancel button if I do not set the DialogResult property. But if I set DialogResult property, the OK button closes the form before I can see the validation errors. I'd like to achieve BOTH: 1. TextBox Validation when OK is clicked, if there is error, do not close the form. 2. If there is no error, the Form2 is closed. From the caller, I want to know whether an user click on OK or Cancel. How do I achieve this? Dion
DionChen wrote: TextBox Validation when OK is clicked Handle the
Validating
andValidated
events on your textboxes. If you look up theControl.Validating
event in MSDN you'll see an example using the events with theErrorProvider
class. Just remember to set the CausesValidation property to false on at least the cancel button, or you can't click it without providing valid data. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation -
Thanks for your help. I definitely can do my validation on btnOK_click event, but how do I know from my main form whether an user clicked on OK button or Cancel button if I do not set the DialogResult property. But if I set DialogResult property, the OK button closes the form before I can see the validation errors. I'd like to achieve BOTH: 1. TextBox Validation when OK is clicked, if there is error, do not close the form. 2. If there is no error, the Form2 is closed. From the caller, I want to know whether an user click on OK or Cancel. How do I achieve this? Dion
-
Hi, In my WinForm App, I have to show another form2. but I do have to know if the user click on OK or Cancel. therefore I assigned DialogResult property of OK and Cancel button on Form2 to OK and Cancel, respectively. It works fine and I can use Dialogresult to figure out whether the OK or Cancel button is clicked to close the Form2. The problem comes in when I add a errorProvider1 to form2, I use it to make sure a couple of textBoxes are not empty and then SetError on ErrorProvider. Because the OK button has DialogResult.OK property set, the dialog closes before I got chance to view the error. Is there anyway to prevent the dialog form close if I have any errors. It is so easy to do in old MFC days. :-(. Dion
In the parent form:
GetMyData diagInfo = new GetMyData(); if (diagInfo.ShowDialog() == DialogResult.Cancel) { cancel logic } else { doit }
The dialog form:public GeMyData { } public string GiveMeInfoOne { get { return info1.Text; } } public string GiveMeInfoTwo { get { return info2.Text; } } private void button1_Click(Object sender, EventArgs e) { if (info1.Text == "" or info2.Text == "") { MessageBox.Show("You really must enter data in my text boxes or I will never go away and I will haunt you the rest of your days"); } else { this.DialogResult = DialogResult.OK; this.Close(); return; } }
Voila -- your dialog never closes until you have a good bit of data in each box and you actually execute a return. :cool: _____________________________________________ The world is a dangerous place.
Not because of those that do evil,
but because of those who look on and do nothing.