Visual Basic 2008 Express Edition - MsgBox's
-
Monday, February 18, 2008 6:32 AM
Hello everyone, i'm currently working on a NotePad program.
I'm trying to make a "New" button that has 3 options:
Save the file, Cancel and go back to the program and Don't save the file + continue.
This is the code i have so far:
Code SnippetPrivate
Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.ClickMsgBox(
"Do you want to save this file?", MsgBoxStyle.Question & MsgBoxStyle.YesNoCancel, "Are you sure?") If MsgBoxResult.Cancel = Windows.Forms.DialogResult.Cancel ThenRichTextBox1.Text = RichTextBox1.Text
End If
If MsgBoxResult.Yes = Windows.Forms.DialogResult.Yes ThenSaveFileDialog1.DefaultExt =
".rtf"SaveFileDialog1.OverwritePrompt =
TrueSaveFileDialog1.Title =
""
ElseIf SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK ThenRichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)
documentTitle = SaveFileDialog1.FileName
End If
If MsgBoxResult.No = Windows.Forms.DialogResult.No ThenRichTextBox1.Text =
"" End If End SubWell i'm stuck on why it isn't working?
Everytime i click the Yes button and it should open the save dialog but it just clears the page.
Everytime i click the No button it clears the page (I want it to do that).
Everytime i click the Cancel button it also clears the page... But i need it to keep the text and close the msgbox!
So i just need to make these functions work.. Any help?
All Replies
-
Monday, February 18, 2008 11:06 AM
FIrst off try not to use MsgBox, this is for VB6 compatability. Use Messagebox.show. Also use a case statement, its cleaner and clearer. Also you show the msgbox and didn't catch the result in a variable of type dialogresult

Heres the revised code:
Code SnippetDim dlgResult As DialogResult
dlgResult = MessageBox.Show("Do you want to save this file?", _"Are you sure?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
Select Case dlgResult
Case Windows.Forms.DialogResult.No
RichTextBox1.SaveFile(SaveFileDialog1.FileName, _RichTextBoxStreamType.RichText)
documentTitle = SaveFileDialog1.FileNameCase Windows.Forms.DialogResult.Yes
SaveFileDialog1.DefaultExt = ".rtf"
SaveFileDialog1.OverwritePrompt = True
SaveFileDialog1.Title = ""Case Windows.Forms.DialogResult.Cancel
RichTextBox1.Text = RichTextBox1.TextEnd Select
Hope this helps -
Monday, February 18, 2008 11:13 AM
Oh yeah. I used to to VB6 before i started on VB 2008.. A few months ago

Thanks for the help, i really appreciate it!

