Member Avatar for Rahul47

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 ?

Recommended Answers

All 5 Replies

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 :

DialogResult.Cancel

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

commented: Exact same code! =D +6

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
Member Avatar for Rahul47

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.

Glad to hear.
Please mark a s solved. Tx.

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.