svbnet
svbnet

Reputation: 1090

Button setting DialogResult automatically?

I have overridden the default ShowDialog() method on this form with this:

    public DialogResult ShowDialog(int itemToEdit) 
    {
        this._itemToEdit = itemToEdit;
        FillForm(Program.AppConfig.Tweets[itemToEdit]);
        return this.ShowDialog();
    }

The form has an "OK" and a "Cancel" button whose click handlers look like this:

    private void cancelButton_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
    }

However, I have another button which does not set the DialogResult property of the form, yet when it is clicked it is somehow set, and the form hides, returning DialogResult.Cancel. I do not want this to be happening.

Edit: Actually, every button in the form makes it return DialogResult.Cancel, regardless of whether it has a Click handler or not.

Upvotes: 1

Views: 160

Answers (2)

Delphi.Boy
Delphi.Boy

Reputation: 1216

Check DialogResult property of that button. It should be None. And make sure that the button Click event is not set to cancelButton_Click

Upvotes: 4

Markus
Markus

Reputation: 22436

Check the CancelButton-property of the form. For the button that is assigned to this property, the DialogResult is set automatically (similar to the AcceptButton property).
Also check the DialogResult property of the other button. It might be set to DialogResult.Cancel and by that close the form and return the DialogResult you observe.

Upvotes: 2

Related Questions