473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

handling dialog box

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

Nov 17 '05 #1
4 2696
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.

<ro****@tauex.tau.ac.il> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Nov 17 '05 #2
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?

Nov 17 '05 #3
<ro****@tauex.tau.ac.il> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.
Nov 17 '05 #4
cheers!
thanx.

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
5
by: Niks | last post by:
I have a list control. On doubleclick of list item, a new dlg opens. Now, i want to handle 'OnEnter Key'also. i.e. When a list item is selected & Enter key is pressed, the new dlg shd open I tried...
1
by: wendy | last post by:
I have got a problem while using the modal dialogs. In my code I need to open a modal dialog from the parent page and I need to enter some values in the dialog and save it. It takes the values...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
15
by: Brett | last post by:
Say I want my installation to popup a user agreement, after they select yes, the install continues. Otherwise, it aborts. I don't think VS.NET offers this. How else can it be done? Also, does...
6
by: Robin Riley | last post by:
Hi, I have a .NET solution that contains a dll project and a tester application project which, of course, invokes the dll. The dll project has exception handling in it. What's happening is that...
0
by: mail2sirshendu | last post by:
I have a class in which I have created an instance QScrollView and added a QDialog inside it using addChild().I have Ok and Cancel button in the Dialog.When either Ok or Cancel is clicked the...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
1
by: JRC903 | last post by:
Dear Folks: I am current working on a project involving trying to find the cause of some erratic behavior in an old Borland C++ 4.5 database application. Prehaps for obvious reason the orginal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.