![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 12
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#2 |
|
Newbie
Join Date: Nov 2004
Posts: 5
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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()
EndIfThat should do it. |
|
|
|
|
|
#4 | |
|
Newbie
Join Date: Nov 2004
Posts: 5
Rep Power: 0
![]() |
Quote:
I wasn't aware of the whole retVal concept. Now I am. O_O |
|
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Glad I could help. And unlike most of my code, I think that's bug-free as well.
|
|
|
|
|
|
#6 | |
|
Newbie
Join Date: Nov 2004
Posts: 1
Rep Power: 0
![]() |
Quote:
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![]() |
|
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
But then you can't use Yes, No and Cancel...
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|