Click here to Skip to main content
15,883,758 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi ,

I have following requirement.

i have created the Main form say login form. Once the user is logged in . i have created one more form called receptionist form by clicking the login button event from mail form.

from Admin Form i have called the mail form(kind of signout from admin and returning back to main form). In main form on ExitButton event i have called this.Dispose() and the code is not gracefully terminated. I was expecting that all the open forms will be closed, but i am not release from the program and it is in debug mode only.
Posted
Comments
BillWoodruff 3-Jan-15 15:21pm    
You mention: Main Form (could be login form ?), (possibly separate?) Login Form, Mail Form, Receptionist Form, Admin Form.

What you describe above is confusing: it is not clear which Form you open first, which is the Main Form, which are secondary Forms.

Please edit your question, and make the order in which Forms are created clear.

In addition to Solutions 1 and 2:

I don't believe any of your forms are child forms, or any of the forms are the parents of some other forms, unless you use MDI (I doesn't look like you do). In Forms, the parent-child relationship (defined by the property System.Windows.Forms.Control.Parent) is effectively done defunct: if you try to assign a form's parent, and exception will be thrown. This can be worked around by assigning the property TopLevel to false, but it does not make practical sense.

The forms do have important relationship owner-owned (Form.Owner) and one form is different: the main one. This is the form instance which was used in the call to Application.Start. When this form is closed, application exists. This can be used as one more technique to show some "starting" point: your call Application.Start with one form, then close it and make application exit, then start application again with some other form. But the use of the modal form (Solution 1) could usually be much simpler. This is how:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
BillWoodruff 3-Jan-15 16:54pm    
Well, of the three psychics responding here so far, I'd pick your crystal ball :)
Sergey Alexandrovich Kryukov 3-Jan-15 17:29pm    
Do you think that OP's question is too vague for this information? While the problem itself is not quite clear, I think the root causes of it are clear enough.
—SA
BillWoodruff 3-Jan-15 17:36pm    
Yes, I think this question is too vague, and the question does not deserve the benefit of the powerful intellects of you, MacCutchan, and Zorgo ... yet.

But, I am in no way finding fault with any of the content in the posts by you three Wise-Men !

I see this example of vague question, and quick responses by three "major high-quality players" on QA, as symptomatic of a structural issue with CodeProject QA.

Note that I am not saying that a vague question is inherently bad, or one that should be ignored. Au contraire, I think that often the most valuable thing we can do is to assist the OP in clarifying their thought: often, imho, that is the "real" solution, and just as valuable as "the code."

But, my specific proposals to constructively address what I perceive as this issue, expressed several times in the Suggs/Buggs forum, have not yielded any result; I am not particularly bothered by that :)
Sergey Alexandrovich Kryukov 3-Jan-15 17:40pm    
I think you concern about some not existing or non-essential problem. I myself avoid answering too vague questions, or question where I feel the inquirer is not ready to, then I would avoid wasting time. In this case, just some clarifications on form and application behavior related to closing should certainly help. You see, I don't think that answering exact question is important, but helping is important, which is not the same.
—SA
this is the current form in this case. It is not wise to dispose current form directly, but calling Close() will also dispose it by default. Still, as only a singel form is main form closing that one will exit the application if no other properly set up child form is open.
You can exit the application by calling Application.Exit();[^]. You can also call Environment.Exit();[^], but taht's more drastic.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Jan-15 17:19pm    
Good advice. Your "by default" is a just bit inaccurate wording: you cannot really choose default and non-default action on closing. (I was surprised to see it; though I don't know some way to avoid disposal, but in fact, disposal is not happening for MDI forms and hidden forms). Voted 4.
Besides, I added some background information on form relationships and exiting of the application on closing; I and explained one more method of having initial form (log on, splash, whatever) — please see Solution 3.
—SA
Zoltán Zörgő 3-Jan-15 17:33pm    
You can stop propagation, thus prevent disposal this way:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}


LinqPad demo:

var f = new Form();
f.Closing += (s,e) => { (s as Form).Hide(); e.Cancel = true; };
f.ShowDialog();
f.ShowDialog();
Sergey Alexandrovich Kryukov 3-Jan-15 17:36pm    
Yes, of course, but this is not closing at all. In relation to your answer, we discussed only the method Close().
You still cannot say that "calling Close() will also dispose it by default". You should have said that it disposes the form, excluding the cases of MDI child or hidden form.
—SA
Calling this.Dispose will destroy the object that calls it and leave your application in an indeterminate state.

It is not clear exactly how your forms are connected, but I would suggest a better model would be to use a DialogForm as the login, and leave the main form to continue after the login succeeds.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Jan-15 17:21pm    
Probably, by "DialogForm" you meant to say something different: Form.ShowDialog. I voted 4 and "fixed" this in my answer.
Besides, I added some background information on form relationships and exiting of the application on closing; I and explained one more method of having initial form (log on, splash, whatever) — please see Solution 3.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900