Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   Message Box (http://www.programmingforums.org/showthread.php?t=1347)

mgoku21 Nov 29th, 2004 11:12 PM

I got a problem. Im confused on how to manipulate a MessageBox using an IF statment. I need it to close the program when I hit Cancel button and to run a SaveDialogBox when I hit OK. Just cant figure it out. Thanks.

nerdfiles Nov 30th, 2004 12:50 AM

Here is the code I have now:
:

Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
    Dim datBalance As StreamWriter
    Dim dgrResult As DialogResult

    MessageBox.Show("Do you wish to save?", "Save?", _
      MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)

    dlgSave.InitialDirectory = Application.StartupPath
    dgrResult = dlgSave.ShowDialog()
    If dgrResult = DialogResult.OK Then
      datBalance = New StreamWriter(dlgSave.FileName)
      datBalance.WriteLine(FormatCurrency(mdecBalance, , , TriState.False))
      datBalance.Close()
    Else
      Me.Close()
    End If

    Me.Close()
  End Sub


Ooble Nov 30th, 2004 1:44 PM

I suggest using YesNoCancel, where Yes saves and exits, No just exits, and Cancel stops the app from exiting.

Now, I'm not 100% sure in VB .NET, but in VB6, you need to get the return value from the message box and test it for vbYes, vbNo or vbCancel:

:

Dim retVal as Integer

retVal = MessageBox.Show("Do you wish to save?", "Save?", _
    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
If retVal = vbYes Then
  DoSaveStuff()
  Me.Close()
ElseIf retVal = vbNo Then
  Me.Close()
EndIf


That should do it.

nerdfiles Nov 30th, 2004 7:08 PM

Quote:

Originally posted by Ooble@Nov 30 2004, 06:44 PM
I suggest using YesNoCancel, where Yes saves and exits, No just exits, and Cancel stops the app from exiting.

Now, I'm not 100% sure in VB .NET, but in VB6, you need to get the return value from the message box and test it for vbYes, vbNo or vbCancel:

:

Dim retVal as Integer

retVal = MessageBox.Show("Do you wish to save?", "Save?", _
 * * * * MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
If retVal = vbYes Then
 * * DoSaveStuff()
 * * Me.Close()
ElseIf retVal = vbNo Then
 * * Me.Close()
EndIf


That should do it.

And it did it, thanks a bunch.

I wasn't aware of the whole retVal concept.

Now I am. O_O

Ooble Dec 1st, 2004 12:40 PM

Glad I could help. And unlike most of my code, I think that's bug-free as well.

Pino Dec 7th, 2004 6:21 AM

Quote:

Originally posted by nerdfiles+Dec 1 2004, 12:08 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (nerdfiles @ Dec 1 2004, 12:08 AM)</td></tr><tr><td id='QUOTE'> <!--QuoteBegin-Ooble@Nov 30 2004, 06:44 PM
I suggest using YesNoCancel, where Yes saves and exits, No just exits, and Cancel stops the app from exiting.

Now, I'm not 100% sure in VB .NET, but in VB6, you need to get the return value from the message box and test it for vbYes, vbNo or vbCancel:

:

Dim retVal as Integer

retVal = MessageBox.Show("Do you wish to save?", "Save?", _
 * * * * MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
If retVal = vbYes Then
 * * DoSaveStuff()
 * * Me.Close()
ElseIf retVal = vbNo Then
 * * Me.Close()
EndIf


That should do it.

And it did it, thanks a bunch.

I wasn't aware of the whole retVal concept.

Now I am. O_O [/b][/quote]
You could just do it

:

If MsgBox("Question", vbOKCancel, "Title") = vbOK Then
'etc etc


:)

Ooble Dec 8th, 2004 1:44 PM

But then you can't use Yes, No and Cancel...

Rory Jan 11th, 2005 8:15 PM

Message Box
 
What about:
:

Select Case MsgBox("Do you want to lay off all your employees, Bill?", vbExclamation + vbYesNoCancel, "Microsoft Corporation")
Case VbYes
Call FireEveryone
Case VbNo
Call PayEveryoneABonus
Case VbCancel
Call FallAsleep
End Select


Ooble Jan 12th, 2005 10:46 AM

Stop dragging up old posts already.


All times are GMT -5. The time now is 2:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC