Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27196

VS 2012 messagebox cancel button closes form

$
0
0
Ok so I am trying to make it so that when my form closes and there is text in any of the text boxes it will ask you to save. I have most of it worked out but whenever I click cancel it either keeps popping up the same messagebox repeatedly otherwise if I click "yes" to open the savefiledialog and then select "cancel" and than "cancel" on the message box again it closes the program.
I'd like it to just return to the main form...

Here's the code that I have (I wasn't sure where the problem is exactly so here's all the code for the form...) I do know it won't let me e.cancel = true for the cancel button it throws an error.

Code:

Imports System.IO

Public Class mainWindow

    Private Sub checkTextBoxes(e As System.ComponentModel.CancelEventArgs)                                          'THIS IS PART OF THE PROBLEM

        'Declare the variables
        Dim ctrl As Control
        Dim firstName As String = txtFirstName.Text
        Dim lastName As String = txtLastName.Text

        'Check all the text boxes
        For Each ctrl In Me.Controls
            If (TypeOf ctrl Is TextBox) Then
                If ctrl.Text <> "" Then

                    'Set sfdmainWindow properties
                    With sfdmainWindow
                        .DefaultExt = "rei"
                        .FileName = firstName & " " & lastName & " " & "-" & " " & Today()
                        .Filter = "Reichel Lead Log File (*.rei) | *.rei"
                        .FilterIndex = 1
                        .OverwritePrompt = True
                        .Title = "Reichel Insulation - Lead Log"
                    End With

                    'Declare variables
                    Dim msg As String
                    Dim title As String
                    Dim style As MsgBoxStyle
                    Dim response As MsgBoxResult

                    msg = "Would you like to save before quitting?"  ' Define message.
                    style = MsgBoxStyle.DefaultButton3 Or MsgBoxStyle.Question Or MsgBoxStyle.YesNoCancel
                    title = "Reichel Insulation - Lead Log"  ' Define title.
                    ' Display message.
                    response = MsgBox(msg, style, title)

                    If response = MsgBoxResult.Yes Then
                        sfdmainWindow.ShowDialog()
                    End If
                    If response = MsgBoxResult.No Then
                        End
                    End If
                    If response = MsgBoxResult.Cancel Then
                        e.Cancel = True                                                                    'THIS IS PART OF THE PROBLEM
                    End If
                End If
            End If
        Next ctrl
    End Sub

    'INSERT TOOLTIPS ON FORM
    Private Sub ToolTips()
        Me.toolTmainWindow.SetToolTip(Me.cbxRep, "Who is the sales rep that will handle this customer estimate?")
        Me.toolTmainWindow.SetToolTip(Me.cbxJobType, "What is the main job type for this estimate?")
        Me.toolTmainWindow.SetToolTip(Me.cbxSource, "Where did the customer hear about us?")
        Me.toolTmainWindow.SetToolTip(Me.txtSource, "If the customer heard about us from a source" & ControlChars.NewLine & "other than what's listed please enter it here.")
        Me.toolTmainWindow.SetToolTip(Me.txtFirstName, "What is the Customers first name?")
        Me.toolTmainWindow.SetToolTip(Me.txtLastName, "What is the Customer last name?")
        Me.toolTmainWindow.SetToolTip(Me.txtAddress1, "What is the customers address?")
        Me.toolTmainWindow.SetToolTip(Me.txtAddress2, "What is the customers city?")
        Me.toolTmainWindow.SetToolTip(Me.txtZipCode, "What is the customers zip code?")
        Me.toolTmainWindow.SetToolTip(Me.cbxState, "What is the customers state?")
        Me.toolTmainWindow.SetToolTip(Me.txtBusinessName, "What is the name of the customers business name?")
        Me.toolTmainWindow.SetToolTip(Me.txtEmail, "What is the customers email address?")
        Me.toolTmainWindow.SetToolTip(Me.cbxTimeHour, "Enter the hour that the customer called.")
        Me.toolTmainWindow.SetToolTip(Me.cbxTimeMinute, "Enter the minute range that the customer called.")
        Me.toolTmainWindow.SetToolTip(Me.cbxTimeAMPM, "Enter the AM/PM when the customer called.")
        Me.toolTmainWindow.SetToolTip(Me.txtComments, "Please fill out as much information about the job that we will be bidding" & ControlChars.NewLine & "along with any additional jobs that will be doing")
    End Sub

    'CLEAR THE TEXT BOXES ON THE FORM
    Private Sub ResetForm(ByVal root As Control)
        For Each ctrl As Control In root.Controls
            ResetForm(ctrl)
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Text = String.Empty
            End If
            If TypeOf ctrl Is ComboBox Then
                CType(ctrl, ComboBox).Items.Clear()
            End If
        Next
        monCalendar.SelectionStart = Today
    End Sub

    'WHAT HAPPENS WHEN THE FORM CLOSES
    Private Sub mainWindow_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        checkTextBoxes()                                                                                                                                            'THIS IS PART OF THE PROBLEM
    End Sub

    'WHAT HAPPENS WHEN THE MAIN FORM LOADS
    Private Sub mainWindow_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Show the startWindow form
        startWindow.ShowDialog()
        ToolTips()
    End Sub

    'WHAT HAPPENS WHEN THE SAVE BUTTON IS CLICKED
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        'Set Variables
        Dim firstName As String = txtFirstName.Text
        Dim lastName As String = txtLastName.Text

        'Set sfdmainWindow properties
        With sfdmainWindow
            .DefaultExt = "rei"
            .FileName = firstName & " " & lastName & " " & "-" & " " & Today()
            .Filter = "Reichel Lead Log File (*.rei) | *.rei"
            .FilterIndex = 1
            .OverwritePrompt = True
            .Title = "Reichel Insulation - Lead Log"
        End With

        'Save the file
        sfdmainWindow.ShowDialog()
    End Sub

    'WHAT HAPPENS WHEN THE SOURCE COMBO BOX ITEM IS CHANGED
    Private Sub cbxSource_SelectedValueChanged(sender As Object, e As EventArgs) Handles cbxSource.SelectedValueChanged
        If cbxSource.SelectedItem = "Other" Then
            txtSource.Enabled = True
        Else
            txtSource.Enabled = False
        End If
    End Sub

    'WHAT HAPPENS WHEN THE CANCEL BUTTON IS CLICKED
    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        checkTextBoxes()                                                                                                                  'THIS IS PART OF THE PROBLEM
    End Sub

    'WHAT HAPPENS WHEN THE HOUR SELCTION IS CHANGED
    Private Sub cbxTimeHour_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxTimeHour.SelectedIndexChanged
        cbxTimeMinute.Enabled = True
    End Sub

    'WHAT HAPPENS WHEN THE MINUTE SELECTION IS CHANGED
    Private Sub cbxTimeMinute_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxTimeMinute.SelectedIndexChanged
        cbxTimeAMPM.Enabled = True
    End Sub

    'RESET THE FORM TO THE DEFAULTS BY CLICKING THE FILE-NEW ITEM
    Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click

        'Clear the textboxes
        ResetForm(Me)

        'Set the focus to the Sales Rep ComboBox
        cbxRep.Focus()
    End Sub

    'ENABLE THE SAVE BUTTON --- TEXTBOX FIRST NAME
    Private Sub txtFirstName_TextChanged(sender As Object, e As EventArgs) Handles txtFirstName.TextChanged, txtLastName.TextChanged
        If txtFirstName.TextLength = 0 Then
            btnSave.Enabled = False
        Else
            btnSave.Enabled = True
        End If
    End Sub

    'OPEN FILE DIALOG MENU STRIP
    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
        ofdmainWindow.ShowDialog()
    End Sub

    'SAVE FILE DIALOG MENU STRIP
    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
        sfdmainWindow.ShowDialog()
    End Sub

    'EXIT PROGRAM MENU STRIP
    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        checkTextBoxes()                                                                                                                                  'THIS IS PART OF THE PROBLEM
    End Sub
End Class


Viewing all articles
Browse latest Browse all 27196

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>