Suppose we are coding a messagebox on closing event of a form.
It Shows three buttons Yes,No and Cancel. Now I wish to code for CANCEL button.

How can I achieve this ?

5 Years
Discussion Span
Last Post by lolafuertes

I'm not exactly sure how to do this off the top of my head. have you seen if the message box returns a dialog result. I know alot of of .net dialogs have a dialog result. if you designed the message box yourself you can go to the properties of the cancel button and select the dialog result to be :


An example by Darin Dimitrov on StackOverflow:

Dim result = MessageBox.Show("message", "caption", MessageBoxButtons.YesNoCancel)
If result = DialogResult.Cancel Then
    MessageBox.Show("Cancel pressed")
ElseIf result = DialogResult.No Then
    MessageBox.Show("No pressed")
ElseIf result = DialogResult.Yes Then
    MessageBox.Show("Yes pressed")
End If

Hope this helps

Edited by lolafuertes

Votes + Comments
Exact same code! =D

Something like this

  Dim diares As Windows.Forms.DialogResult = MsgBox("Test", MsgBoxStyle.YesNoCancel)

  If diares = Windows.Forms.DialogResult.Yes Then
      MsgBox("Yes pressed")
  ElseIf diares = Windows.Forms.DialogResult.No Then
      MsgBox("No pressed")
  ElseIf diares = Windows.Forms.DialogResult.Cancel Then
      MsgBox("Cancel pressed")
  End If

Thank You Fellas for your contribution.
I have fortunatley figured it out how to achieve that functionality and I am encliosing following code for you all to refer in future.

Private Sub frmQuestion_Paper_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim msg As DialogResult = MessageBox.Show("Do you want to save the changes made to Question Paper?", "Warning !! ", _
        MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        If msg = DialogResult.Cancel Then
            MessageBox.Show("Sorry !! ")
            e.Cancel = True
        End If
    End Sub

As you can see e.Cancel= True cancel the calling of FormClosing Event and hence Form is saturated in it's current state.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.