开发者

c# AcceptButton and validation

开发者 https://www.devze.com 2023-01-17 10:23 出处:网络
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 t

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 开发者_StackOverflowway to prevent form from closing if validation fails without removing acceptbutton properties ??


This is the working solution we use (and it's kinda compiled from other answers).

You just have to set the DialogResult to None to prevent the form from closing.

//form init, auto-generated code (this is the case described)
private void InitializeComponent()
{
    //....
    this.AcceptButton = btnOk;
    this.btnOk.DialogResult = DialogResult.OK;
    //....
}

//event handlers
private void btnOK_Click(object sender, EventArgs e)
{
    if (!Validate())
        this.DialogResult = DialogResult.None;
}

private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this.DialogResult == DialogResult.None)
        e.Cancel = true;
}


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


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;


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


Do not set Button.DialogResult in the Designer or somewhere else (don't confuse with Form.DialogResult). In the Button's click handler do the validation and if successful set Form.DialogResult to DialogResult.OK and close the form.

private void OkBtn_Click(object sender, EventArgs e)
{
    if (isValid())
    { 
      this.DialogResult = DialogResult.OK;
      this.Close();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号