Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I make a code to exit winform C#, it works but I must click "yes" in messagebox twice. What wrong with it?
Here is my code:
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("Sure?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Application.Exit();
            }
            else
            {
                e.Cancel = true;
            }
        }
Posted

Hi Create object for that form,using obj u can close form.Instead of using Application.Exit use close.

C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        DialogResult result = MessageBox.Show("Sure?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (result == DialogResult.Yes)
        {
             Form1 ff= new Form1() ;
             ff.Close ();

        }
        else
        {
            e.Cancel = true;
        }
    }
 
Share this answer
 
Comments
Member 10390715 29-Jul-15 2:20am    
Your code dose not work. I click "yes" many time but it still question me "Sure?"
Ralf Meier 29-Jul-15 3:38am    
There is no Action necessary by DialogResult=Yes - only in case of No - please efer to my solution (1)
Aravindba 29-Jul-15 4:37am    
i try ur code ,it never close window,again and again it show dialogbox,and first read the question in which language they ask,here they ask in C# in question itself mention as C# then why u give solution in vb ?
Ralf Meier 29-Jul-15 5:16am    
I have updated my solution to C# too.
But I think, what to be done is to seen in the VB-Code too.
I don't know, why the code is not working for you - on my System it is working properly ...
Aravindba 29-Jul-15 2:29am    
hi download exe form this link and check,when u click close buttom in form it ask yes or no,if yes it close window,if no it not close windows
https://www.dropbox.com/s/4ybb06j1fq3efp2/WindowsFormsApplication1.exe?dl=0
Try it like this :
VB
Protected Overrides Sub OnFormClosing(e As System.Windows.Forms.FormClosingEventArgs)
     Dim myResult As DialogResult = MessageBox.Show(Me, "FormClosing-Message", "Caption-Text", MessageBoxButtons.YesNo)

     If myResult = Windows.Forms.DialogResult.No Then
         e.Cancel = True
     End If
     MyBase.OnFormClosing(e)
 End Sub


I don't use the Event but the Event-calling-Method.
You don't Need to do anything when DialogResult is "Yes" - it is the normal case ...

and additional as C#-Code :
C#
protected override void OnFormClosing(System.Windows.Forms.FormClosingEventArgs e)
{
	DialogResult myResult = MessageBox.Show(this, "FormClosing-Message", "Caption-Text", MessageBoxButtons.YesNo);

	if (myResult == Windows.Forms.DialogResult.No) {
		e.Cancel = true;
	}
	base.OnFormClosing(e);
}
 
Share this answer
 
v2

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