Msgbox Choice

lothos12345

Junior Contributor
Joined
May 2, 2002
Messages
294
Location
Texas
I want to develop a decision structure using the msgbox. For example when the user clicks "Close" I want a msgbox to pop up asking are you sure you wish to exit program? I want the msgbox to have "yes/no" buttons. If the user clicks "no" I want it to return them to the form, if the user clicks "yes" I want it to close out the program. Not quite sure how to accomplish this, any help with this would be greatly appreciated.
 
First of all, go the .NET way and use MessageBox.Show instead of
MsgBox.

The way to accomplish this is with a simple Select Case statement.

First show your messagebox, get the value, then use the Select
Cast to decide what to do.

Visual Basic:
Dim dr As DialogResult

dr = MessageBox.Show("Wanna exit?", "Exit?", MessageBoxButtons.YesNo)

Select Case dr
  Case DialogResult.Yes
    ' User clicked Yes, exit the program

  Case DialogResult.No
    ' User clicked No, return to the form (do nothing)

End Select

I set the return value of the MessageBox to a variable, dr, but
you could just select the case of the call to the messagebox
itself, i.e.:

Visual Basic:
Select Case MessageBox.Show("Wanna exit?", "Exit?", MessageBoxButtons.YesNo)

It cleans up the code a bit, but it might be a little confusing if you
are new to these concepts.
 
Back
Top