I am working on a c# winform application and i have a button that on click a dialog asking for password appears. If the password is correct then the code under the button is executed. Here is the code on the main form.
password pass = new password();
pass.ShowDialog();
if (pass.DialogResult == DialogResult.OK)
{
if (pass.Password == "12")
{
And below is the code that i have on the OK button in the password form
private void btnOK_Click(object sender, EventArgs e)
{
button1.DialogResult = DialogResult.OK;
}
The problem is that the OK button of the password form has to be clicked twice and is working. Any ideas why ?
From the Button.DialogResult documentation I see that the property defines what DialogResult to return to the Form when the button is clicked. This means you should have the code line
button1.DialogResult = DialogResult.OK;in the constructor, or simply set it in the designer, instead of on the click-event.