Resolved msgbox help

Joe1

Active member
Joined
Dec 4, 2012
Messages
37
Programming Experience
Beginner
I currently have this code for a vbOKCancel message box.


MsgBox("Load window?", vbOKCancel)

If vbOK Then

RaiseWindow()

End If


If vbCancel Then

Exit Sub (I want this line to cancel/close the message box)
End If

However this causes the WinForm "Window" to open whichever button is clicked? Is there something wrong with this?
I have also tried using If>Elseif but get the same results


 
Last edited:
I currently have this code for a vbOKCancel message box.


MsgBox("Load window?", vbOKCancel)

If vbOK Then

RaiseWindow()

End If


If vbCancel Then

Exit Sub (I want this line to cancel/close the message box)
End If

However this causes the WinForm "Window" to open whichever button is clicked? Is there something wrong with this?
I have also tried using If>Elseif but get the same results


First off, whenever posting code please wrap it inside [xcode=vb] code here [/xcode] tags as it makes it far easier to read.

As for your situation there's a major logic flaw, you're showing a MsgBox but you're not getting the response from it, then you're expecting the following If statement to know what the user clicked. So your first step should be to store the response from the MsgBox call to a variable, then you can use an If statement to check that variable against a response you're looking for, in this case I would assume the response you want to look for is the vbOK.

I would like to add emphasis on the using of the Messagebox class, as 22-degrees has already mentioned. The return value from MessageBox.Show() is one of the DialogResult enumeration members, which can allow you to code with a little more flexibility. For example:
Select Case MessageBox.Show(......)
  Case DialogResult.OK
    'Code for when they've clicked the OK button
  Case DialogResult.Cancel
    'Code for when they've clicked the Cancel button

  '... other cases
End Select
 
Back
Top