how can i exit from the form after clicking the "yes" button of a messageBox??

Recommended Answers

All 9 Replies

Do not expect us to guess and solve your problem.

What is the code that you are working on ?

If MsgBoxResult.Yes Then
 Me.Hide()'or me.dispose : me.close 
End If

are you really don't know how to do it?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim msg As MsgBoxResult
        If MsgBox("Do you really want to exit ?", MsgBoxStyle.YesNo, "Exit") = msg.Yes Then
            Me.Close()
        End If
    End Sub
commented: agreed +8
commented: see.. +4

Its depends on your form, if your form is startup form of project you can use hide method otherwise you can use close method.
If your form is startup form and you use close method then you project is closed.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult = MessageBox.Show("R U Sure To Close the Form", "Closing Form", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (DialogResult == DialogResult.OK)
            {
                Form1 f = new Form1();
                f.Close();
            }
            else
            {
                e.Cancel = true;
            }
        }

Use this for Message Box based Closing of a form.

commented: c# code in vb.net forum :'( +0

If, your doubt is Cleared plz make this thread as Solved.

commented: not experienced enough to ask for that. -2

@Saikalyankumar this is for vb.net forum... thank you :)

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        DialogResult = MsgBox("R U Sure, to Close the Form", MsgBoxStyle.OkCancel, "Confirmation")
        If (DialogResult = Windows.Forms.DialogResult.OK) Then
            Dim f As New Form1
            f.Close()
        Else
            e.Cancel = True
        End If
    End Sub

By mistake i have given the C#.Net code, this code is Equivalent VB Code for MsgBox Closing of Form.

thank you very much to all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.