0

I have crated a windows form application.

VS2012 created a form1 form to me

then I crated a second form called Main

there is a button on form1 form to go to Main form

when I run the application using vs2012, and then I click the x button at the top right of form form1, the form close and I can start working on vs. but the problem is when I click the x button at the top right of the form Main, I can't start working on VS, but I need to click Stop Debugging red button in the vs in order to be able to type in vs.

so my question is when I give this application to my customers, and when the click the x button at the top right of the form, I need the application to close totally, I don't need them to go the task manager and close the application from it.

Agnieszka Polec
  • 1,471
  • 6
  • 20
  • 26

5 Answers5

1

Your application starts with Form 1, when you close it you are closing the application. Main is just a form, when you close it you are closing the form. What you need to is, go to FormClosing event of Main form and add

Application.Exit();

Sefa
  • 8,865
  • 10
  • 51
  • 82
  • your solution works perfectly. however the other guy told me to use `System.Envirenement.Exiit(0)` which one is better pelease? Sorry I can't +1 to you because I don't have enough reputation – Agnieszka Polec Jul 24 '14 at 11:58
  • Here is a detailed article: http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx – Sefa Jul 24 '14 at 12:02
  • System.Envirenement.Exit(0); quite evrything related to your app wich means anything that your app opend will but closed it will the process ;) – Youness Jul 24 '14 at 12:02
  • @Youness that is a pain, please tell me what is the good one. I just need to close the application. the application write to some csv files – Agnieszka Polec Jul 24 '14 at 12:05
  • @AgnieszkaPolec i don't think it will make difference in your application. Your question depends on the program. You can just the code i gave. – Sefa Jul 24 '14 at 12:08
  • he is right for you it would'nt make a défference you can use wich one you want – Youness Jul 24 '14 at 12:09
  • Quote: _Application.Exit()....As a general guideline, use this call if you have called System.Windows.Forms.Application.Run._ This applies to your program. – TaW Jul 24 '14 at 19:17
0

got to the FormClosing event and put this code :

System.Envirenement.Exit(0);
Youness
  • 1,468
  • 1
  • 9
  • 19
  • where is that? a property in vs ? – Agnieszka Polec Jul 24 '14 at 11:55
  • its an Event whene a jsut select your form and got to the proprietes tab and click events (the yellow thender ;) ) there look for FormClosing double click the empty field near it and a method will be added write that code int it – Youness Jul 24 '14 at 12:01
  • Don't do that. It is a hard exit, requiring special rights. See the docs! – TaW Jul 24 '14 at 19:19
0

What do you do with form1 when you go to Main? If you just hide it in order to show the other, then close the other one, while just keeping form1 hidden, then form1 will naturally still be running, even if Main is closed.

Sounds like the logical thing to do would be to go back to form1 from Main when you close the latter - otherwise, you can close both using Application.Exit(0) on the appropriate event.

In either case, it would be easier to answer this if you had provided some actual code.

Update:

You could also use System.Environment.Exit(0), but that might require more privileges, which might or might not affect your situation. Again - some code would be helpful.

In regard to your comments, see this answer for more info about termination options.

Community
  • 1
  • 1
Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • thanks for your answer. but should I use `System.Envirenement.Exit(0)` or `Application.Exit();` please? – Agnieszka Polec Jul 24 '14 at 12:07
  • I believe `dispose` will only close the window - not sure, but it might not stop the application completely (test it and see what happens!). `Application.Exit()` should be fine; it should stop your program, like you want. Also, see my update - this has already been answered quite well on SO. – Kjartan Jul 24 '14 at 12:19
  • My pleasure! PS: Feel free to upvote or mark an answer as your "accepted answer" if it solves your problem ;) – Kjartan Jul 24 '14 at 13:09
  • Sorry I am not able to upvote since I don't have enough reputation for that. Plus, I had to accept the first correct answer. I wish I could accept more. many thanks for the efforts – Agnieszka Polec Jul 24 '14 at 14:07
0

Problem is because your Form1 form still running(hidden).
If you use Form1 only for open your Main form. Then delete Form1 and set Main as StartUp form in Properties of your proejct:
Application -> StartUp form

After this when you close Main form application will be closed properly

If you use Form1 then you need close Form1 too after you close Main form.
Use FormClosed eventhandler for this purpose

Fabio
  • 31,528
  • 4
  • 33
  • 72
0

Right click on Main form -> Properties
on the Event tab choose FormClosing event
and write this code

private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
           System.Environment.Exit(0);
        }
ASalameh
  • 783
  • 1
  • 8
  • 29