Why do buttons need to be pressed twice to work in this modal dialog?
I have this C# code:
public partial class Continue : Form
{
public Continue(string colourName)
{
InitializeComponent();
lblMessage.Text = String.Format("Do you wish to change the colour to {0}", colourName);
}
private void btnConfirm_Click(object sender, EventArgs e)
{
btnConfir开发者_运维技巧m.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
btnCancel.DialogResult = DialogResult.Cancel;
}
}
It works fine, but when the dialog pops up it requires me to click on a button twice to use it. Does anyone have an idea why?
You need to set this.DialogResult
rather than btnxxx.DialogResult
in the Click handlers, or set the btnxxx.DialogResult before the handlers.
The form's DialogResult is set to the button's before the Click method is run, so the first time the event is run the Form's DialogResult is left at None, but the second time it is set to the (now-set) button's DialogResult.
精彩评论