-1

Form2 is user confirmation window,i want to close the application by stop button on mainform,but currently form2 is active and there is no dependency between form2 and mainform.How to close application ?

Sonam Jain
  • 11
  • 1
  • 5
    possible duplicate of [How to properly exit a C# application?](http://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application). `Application.Exit` exits the application, it doesn't matter from where you call this method. – Shaharyar Sep 21 '15 at 09:43
  • Application.Exit also not working.form2 not allowing to click button(not cross button) on mainform. – Sonam Jain Sep 21 '15 at 09:52
  • That is because you must be opening `form2` using `ShowDialog()`. It doesn't allow to access parent untill it closes, you'll have to use `form2.Show()` to keep `mainform` in access. – Shaharyar Sep 21 '15 at 09:55

4 Answers4

1

You must be opening Form2 using ShowDialog() method. Instead of that use Show() method and on MainForm button's click event call Application.Exit() method

Rakesh
  • 228
  • 1
  • 6
  • using show() creating other problem i my application.Is there any other way to solve this? – Sonam Jain Sep 22 '15 at 09:22
  • Hi, if Modal form is open then you can not click on Main (Parent) form. – Rakesh Sep 22 '15 at 09:48
  • What is the problem with earlier solution – Rakesh Sep 22 '15 at 09:48
  • Please let me know the whole scenario. – Rakesh Sep 22 '15 at 10:30
  • if i will use show() method then continuously form2 pop-up will come one by one without giving response(button click) for checking different alert.so i am not using show() method.I am calling form2 showDialog from mainform class. – Sonam Jain Sep 22 '15 at 12:19
  • Suggestion: create a single instance of Form2 and on main form's closing event check whether you have received a response for particular alert or not. If NOT then cancel form closing event and show Form2. – Rakesh Sep 23 '15 at 03:53
0

You can use...

// for WinForms application
if (System.Windows.Forms.Application.MessageLoop) 
{

    System.Windows.Forms.Application.Exit();
}

// for Console application

System.Environment.Exit(1);
Nalaka
  • 1,165
  • 7
  • 12
0

So your problem is the parent window will not have focus/input until the modal window (the confirmation dialog) has finished.

So, to avoid this, just make the confirmation dialog using a custom window (instead of a common dialog / msgbox) and show it. This way if the user wants to click your main form they can (the confirmation will be on background, covered by the main window because of this).

beppe9000
  • 1,056
  • 1
  • 13
  • 28
0

it looks like you're showing the dialog as ShowDialog() or showing a built in MessageBox which is also a ShowDialog that needs to be closed in order for focus to return back to main window. So what you should do is create a custom Dialog and use dialog.Show() method. This will allow you to interact with your main window while the dialog is open and you can click on a button to close your application regardless of the dialog being open or not. Following code should do the closing work for you

Application.Current.ShutDown();
WAQ
  • 2,556
  • 6
  • 45
  • 86