views:

67

answers:

3

Hi I have a form which contains of several textboxes and two buttons Cancel and Ok. These buttons are assigned to accept and cancel buttons properties in form. The problem is that I have to validate texts entered in all textboxes.

I want to do that if user click Ok button(which is acceptButton). So I wrote a function which is reponsible for validation and I fire this function if user click Ok button. The problem is that I can't stop form from closing even if validation function return false. It happens because of the fact that I assigned acceptbutto property to my Ok button. Is there any way to prevent form from closing if validation fails without removing acceptbutton properties ??

A: 

Don't set the AcceptButton property in your Form and remove the DialogResult property from your button (set it to DialogResult.None instead).

You can set the DialogResult property of your form in your validation code instead.

 if (allFieldsValidated) {
     DialogResult = DialogResult.OK;
 }
Patrick
I was trying this approuch previously however this code doesn't work. If I click Ok button and validation returns true nothing happen, however if I click button Ok once more, form close itself.
exMode
@exMode: What do you mean nothing happens? Does it not go into validation? When you are done validating, can you not set `DialogResult` of the form to `DialogResult.OK` and have the Form close itself?
Patrick
I mean that form does't close even if validation returns true. Unofrtunatelly I can't resign from seting DialorResult.OK
exMode
But when the validation returns true, why can't you set the value of DialogResult? I do not understand what you mean by "I can't resign from seting", would you please rephrase?
Patrick
I set the dialogResult.Ok when validation returns true. However form doesn't close for the first time. I have to click Ok button once again
exMode
That seems strange. It seems like your setting the DialogResult of the Button and not the Form. Are you really setting the DialogResult of *the Form* that is your dialog to DialogResult.OK and not the Button that you click?
Patrick
+2  A: 

One way could be to apply the validation in OnFormClosing event and cancel the action based upon validation result.

--EDIT--

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this.DialogResult == DialogResult.OK)
    {
        if (!IsValid())
        {
            Info("Invalid data");
            e.Cancel = true;
        }
        else
        {
            Info("Valid data found, closing dialog");
        }
    }
    else if (this.DialogResult == DialogResult.Cancel)
    {
        Info("Just cancelling!");
    }
}

And you may call this.Close(); on Cancel/Ok events.

Alternatively, in your OK button implementation you can change the DialogResult of the button and set it to None;

acceptButton.DialogResult = DialogResult.None;
KMan
the first snipet works but it also prevents from from closing even if You click cancel button
exMode
@exMode: See my updated code based upon your comment. `Info()` is just a method that calls `MessageBox`
KMan
@KMan: But listening on the Form close event means you set the dialogresult value, let the dialog post an event dispatch, then it gets canceled, and then the dialogresult of the form is changed back to none. Why the extra steps of listening to Form.Close? Just wait and set the value of DialogResult when it's validated, not every time.
Patrick
@Patrick: It does not mean *you set the dialogresult value*. When you press the cancel button the framework does that for you, that, changes the `DialogResult`'s state for you. Try it.
KMan
@KMan: But it still means that the DialogResult on the form is set to that of the Button, even if you don't set it explicitly yourself, which leads to a few extra calls that should be unnecessary. And as a side note, if you have DialogResult set on the button you don't need to call `this.Close()` on the form.
Patrick
+1  A: 

In the Ok button click handler change DialogResult to DialogResult.None when validation fails

Vinay B R
not working :( Form is closing despite the fact that I set DialorResult as You said
exMode