PC Review


Reply
Thread Tools Rate Thread

handling dialog box

 
 
ronenk@tauex.tau.ac.il
Guest
Posts: n/a
 
      7th Jul 2005
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

 
Reply With Quote
 
 
 
 
Sean Hederman
Guest
Posts: n/a
 
      7th Jul 2005
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.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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
>



 
Reply With Quote
 
 
 
 
ronenk@tauex.tau.ac.il
Guest
Posts: n/a
 
      8th Jul 2005
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?

 
Reply With Quote
 
Sean Hederman
Guest
Posts: n/a
 
      8th Jul 2005
<(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.


 
Reply With Quote
 
ronenk@tauex.tau.ac.il
Guest
Posts: n/a
 
      8th Jul 2005
cheers!
thanx.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling output from printer dialog box StrandElectric Microsoft VB .NET 4 30th Jan 2011 01:22 AM
Handling of Save Button procedure in Save As Dialog Box. Kavita Microsoft Excel Programming 1 21st Aug 2008 01:56 PM
How to delete the "Insert Function Dialog Box" (dialog box only)? =?Utf-8?B?VEJJJydkIGJpa2Vy?= Microsoft Excel Worksheet Functions 2 7th Apr 2007 09:18 PM
Error handling with a handling routine =?Utf-8?B?YmVu?= Microsoft Excel Programming 0 15th Mar 2005 04:01 PM
Question about best practices with SqlConnection, error handling and memory handling Lars-Erik Aabech Microsoft ADO .NET 9 17th Apr 2004 06:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:32 PM.