none
Visual Basic 2008 Express Edition - MsgBox's

    Question

  • 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 Snippet

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click

    MsgBox("Do you want to save this file?", MsgBoxStyle.Question & MsgBoxStyle.YesNoCancel, "Are you sure?")

    If MsgBoxResult.Cancel = Windows.Forms.DialogResult.Cancel Then

    RichTextBox1.Text = RichTextBox1.Text

    End If

     

    If MsgBoxResult.Yes = Windows.Forms.DialogResult.Yes Then

    SaveFileDialog1.DefaultExt = ".rtf"

    SaveFileDialog1.OverwritePrompt = True

    SaveFileDialog1.Title = ""

     

    ElseIf SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)

    documentTitle = SaveFileDialog1.FileName

    End If

     

    If MsgBoxResult.No = Windows.Forms.DialogResult.No Then

    RichTextBox1.Text = ""

    End If

    End Sub

     

    Well 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?

    Monday, February 18, 2008 6:32 AM

Answers

  • 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 Smile

     

    Heres the revised code:

     

    Code Snippet

            Dim 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.FileName

                Case Windows.Forms.DialogResult.Yes
                    SaveFileDialog1.DefaultExt = ".rtf"
                    SaveFileDialog1.OverwritePrompt = True
                    SaveFileDialog1.Title = ""

                Case Windows.Forms.DialogResult.Cancel
                    RichTextBox1.Text = RichTextBox1.Text

            End Select

     

     

     

    Hope this helps
    Monday, February 18, 2008 11:06 AM

All replies

  • 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 Smile

     

    Heres the revised code:

     

    Code Snippet

            Dim 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.FileName

                Case Windows.Forms.DialogResult.Yes
                    SaveFileDialog1.DefaultExt = ".rtf"
                    SaveFileDialog1.OverwritePrompt = True
                    SaveFileDialog1.Title = ""

                Case Windows.Forms.DialogResult.Cancel
                    RichTextBox1.Text = RichTextBox1.Text

            End Select

     

     

     

    Hope this helps
    Monday, February 18, 2008 11:06 AM
  • Oh yeah. I used to to VB6 before i started on VB 2008.. A few months ago Stick out tongue

    Thanks for the help, i really appreciate it!

     

    Monday, February 18, 2008 11:13 AM