handling dialog box

R

ronenk

I have this code to load an authentication form once my app is loaded.
I want the authentication form to be closed if a user is authenticated
successfully and to give the option to close app on his decision.

private void MainForm_Activated(object sender, EventArgs e)
{

AuthFrm StartUpfrm = new AuthFrm();

if (StartUpfrm.ShowDialog() == DialogResult.OK)

{
this.Close();
}
if (StartUpfrm.ShowDialog() == DialogResult.Cancel) //Error is here
{
Application.Exit();
}
}


This code is part of authentication form:



private void btnOK_Click(object sender, System.EventArgs e)
{

this.Close();
this.DialogResult = DialogResult.OK;
DAL dal= new DAL();
if (dal.userExists(boxUserName.Text)==true)
this.DialogResult = DialogResult.OK;
else
MessageBox.Show("No such user found");

}

private void btnClose_Click(object sender, System.EventArgs e)
{
Application.Exit();
}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

When I run it & press close button, I get error in the marked line:
"Cannot access a disposed object named 'authentication form name'"
When I press submit button I get nothing. Form stays intact, though
this.close();

Why such a thing may occur?

TIA,

Ronen
 
S

Sean Hederman

Remove the event handler for your Cancel Button, and set it's DialogResult
property in the Forms Designer to be Cancel.

I also feel that if you can avoid using Application.Exit you should. It
causes all sorts of nastiness to happen in your application.
 
R

ronenk

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?
 
S

Sean Hederman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top