Mr_Green
Mr_Green

Reputation: 41832

call a button of different form in c# Forms?

I am new to C#, I am creating a application in which there is a need of using two forms one is Mainform and other is DialogForm.

The DialogForm has two buttons btnYes and btnNo.

whenever the user clicks the close button, FormClosing Event invokes in which I am calling the DialogForm as shown below:

DialogForm ex = new DialogForm();
ex.ShowDialog(this);

Now I want to give e.cancel=false for btnYes and e.cancel=true for btnNo. (this explained by my sir, only basics)

I know how to give functions to a Button which is in same Form but I dont know how to if the Form is different.

I have gone through some links but as I am new to c#, I can't understand it. If you atleast provide me some links that would be appreciable.

Thanks in advance.

Upvotes: 0

Views: 297

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112279

Forms have a property DialogResult. You can set it in the button event handlers.

DialogResult = DialogResult.Yes;
// or
DialogResult = DialogResult.No;

Then you can call the form like this

if (ex.ShowDialog(this) == DialogResult.Yes) {
    // Do something
} else {
    // Do something else
}

You can also set the CancelButton property of the form in the properties window. Microsoft says:

Gets or sets the button control that is clicked when the user presses the ESC key.

The form has also an AcceptButton property. Microsoft says:

Gets or sets the button on the form that is clicked when the user presses the ENTER key.

Upvotes: 1

Related Questions