<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Removed the event handler for Cancel Button & set it's DialogResult
> property in the Forms Designer to be Cancel. Now reaction is like the
> OK button- Noreaction at all.
>
> What would you offer instead of Application.Exit, to make the same
> action?
If the DialogResult property of the button is set, it should use that value
when you click the button as the dialog result of the form. Setting the
dialog result of a dialog should close the form. For example, in your OK
button, this is all that is neccessary:
DAL dal= new DAL();
if (dal.userExists(boxUserName.Text)==true)
this.DialogResult = DialogResult.OK;
else
MessageBox.Show("No such user found");
However, you have a couple of other problems that I hadn't noticed last
night. First off, your dialog display code is in the Activated event. The
activated event is fired whenever the form is activated. Activation is a bit
confusing sometimes. It doesn't mean the first time the form becomes visible
and focused, it means EVERY time the form becomes focused. So, if you show a
dialog inside your activated event, as soon as the dialog is closed the
Activated event will fire again, displaying the dialog again. In such a
situation it is useful to keep a variable indicating if you've activated
before.
In addition, have a look at these two lines:
if (StartUpfrm.ShowDialog() == DialogResult.OK)
if (StartUpfrm.ShowDialog() == DialogResult.Cancel) //Error is here
They're both calling the ShowDialog method, and they're both showing the
dialog. So, what happens is that your dialog is displayed by the first
ShowDialog, you press anything you like, and it then executes the second
ShowDialog, trying to show the dialog that's just been closed. The
ShowDialog method does not store the result of the previous showing of the
dialog, it shows the dialog. Since you have only two options, OK and Cancel,
this is a prime candidate for an else statement. I've reworked the Activated
event for you a bit, it should work now.
private void MainForm_Activated(object sender, EventArgs e)
{
if(!_hasActivated) {
_hasActivated = true;
AuthFrm StartUpfrm = new AuthFrm();
if (StartUpfrm.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(this, "OK");
this.Close();
} else {
MessageBox.Show(this, "Cancel");
this.Close();
}
}
}
Instead of using Application.Exit, I've now just closed the main form, which
will have the effect of shutting down the app (as long as there aren't any
other open forms floating around.
|