[C# and VB.Net] How to close a form after confirmation in Winforms

How to close a form in c#  after confirming

 

 

You can put the following code in any button

 

          if (MessageBox.Show("Are you sure you want to exit?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                               this.Close();
            }

 

In VB.NET you can use the following code

 

 If MsgBox("Are you sure you want to close", MsgBoxStyle.YesNo, "Close Application") = MsgBoxResult.Yes Then

            Me.Close()
        End If