CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: exit on menu

  1. #1
    Join Date
    Dec 2008

    Post exit on menu

    I am finishing up this program and have a small bug with my exit on my menu I created. In my code below, if I click on the exit button on my form or right click the form and click exit, it asks me if I want to exit the form and click no and no issue. When I click on the file and then exit on my menu at the top it asks the same thing but when I click no the dialog box comes up one extra time to ask me if I want to close the form. After I click no the second time it goes away. If I click yes the first time it aks me from the menu screen it goes away no issue so it only appears when I click no the first time from the menu. I am sure this is an easy fix but just need a second pair of eyes...

    #Region " File Menu Events "
        'Add exitFormToolStripMenuItem.click, exitFormToolStripMenuItem1.click to the end of the private sub
        'so the context menu strip works when right clicking on form
        Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click, ExitToolStripMenuItem.Click,
            'Close the form if the system user responds yes
            Dim messageString As String = "Do you want to close the form?"
            Dim buttonDialogResult As DialogResult = MessageBox.Show(messageString, "Quit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
            If buttonDialogResult = Windows.Forms.DialogResult.Yes Then
            End If
        End Sub
        Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
            'Call exit button click event
        End Sub
        Private Sub ColorToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ColorToolStripMenuItem.Click
            'Change the form's back color property
            'Store current form color to the controls' color property
            'Display the color component, and save the color to the form's backcolor property
            ColorDialog1.Color = Me.BackColor
            Me.BackColor = ColorDialog1.Color
        End Sub
        Private Sub FontToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FontToolStripMenuItem.Click
            'Change the font user for the entire form
            'Store current font to the control's font peoperty
            'Display the font component, and save the new font to the form's controls
            FontDialog1.Font = Me.Font
            Me.Font = FontDialog1.Font
            MenuStrip1.Font = FontDialog1.Font
        End Sub
        Private Sub ColorToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ColorToolStripMenuItem1.Click
            'The source control property of a context menu references the control to which the mouse is pointing at the time
            'that the mouse is clicked.
            'It is used to enable changing a specific control's property settings
            'Change color of object
            'Send color to dialog box
            ColorDialog1.Color = ContextMenuStrip1.SourceControl.ForeColor
            'Display new color
            ContextMenuStrip1.SourceControl.ForeColor = ColorDialog1.Color
        End Sub
    #End Region
    #Region " Help Menu and Miscellaneous Events "
        Private Sub noneRetirementPlanRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles noRetirementPlanRadioButton.CheckedChanged,
        retirementStandardRadioButton.CheckedChanged, retirement401ARadioButton.CheckedChanged
            'Declare retirement benefit constants 
            Const RETIREMENT_STANDARD As Decimal = 0.05D
            Const RETIREMENT_401A As Decimal = 0.08D
            'Create a radio button in memory and store the values of sender to it
            Dim checkedRadioButton As RadioButton = CType(sender, RadioButton)
            'Use select case to evaluate the name of the radio button to decide which controls to enable/disable
            Select Case checkedRadioButton.Name
                Case "noRetirementRadioButton"
                    retirementRateDecimal = 0D
                Case "retirementStandardRadioButton"
                    retirementRateDecimal = RETIREMENT_STANDARD
                Case "retirement401ARadioButton"
                    retirementRateDecimal = RETIREMENT_401A
            End Select
        End Sub
        Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
            'Display the About Message box
            Dim messageString As String = "Programmed by Bryan Kruep" & ControlChars.NewLine & "Today's Date/Time:  " & Date.Now.ToString
            Dim titleString As String = "About the Payroll Application"
            'Display output message
            MessageBox.Show(messageString, titleString, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub
    #End Region
    #Region " Function Processes "
        'This function simplifies the compute button's click event by moving the computations fross gross pay to a separate function
        'The function returns a decimal value by storing the computed value of gross pay to the name of the function
        Private Function computeGrossPay() As Decimal
            'Parse textbox values to memory variables
            Dim hoursDecimal = Decimal.Parse(hoursWorkedTextBox.Text, Globalization.NumberStyles.Number)
            Dim payRateDecimal = Decimal.Parse(payRateTextBox.Text, Globalization.NumberStyles.Currency)
            'Compute gross pay
            'Gross pay is computed and returned by assigning a value to the name of the function
            If hoursDecimal <= 40D Then
                computeGrossPay = Decimal.Round(hoursDecimal * payRateDecimal, 2)
                computeGrossPay = Decimal.Round((40D * payRateDecimal) + ((hoursDecimal - 40D) * payRateDecimal * 1.5D), 2)
            End If
        End Function
        'In this function the input parameter grossPayDecimal is passed to the computerFederalTax function
        'The value of federalTaxDecimal is returned to the calling prodcedure with a return statement
        'ByVal sends a copy of the argument's value to the procedure so the procedure does not alter the original copy of the value
        Private Function computeFederalTax(ByVal grossPayDecimal As Decimal) As Decimal
            'Const for Federal Tax Rate decimals
            Const TAX_08_LEVEL As Decimal = 0.08D
            Const TAX_18_LEVEL As Decimal = 0.18D
            Const TAX_28_LEVEL As Decimal = 0.28D
            Const TAX_08_PERCENT As Decimal = 985D
            Const TAX_18_PERCENT As Decimal = 2450D
            'Declare variables used within the function
            Dim federalTaxDecimal As Decimal
            'Compute the federal tax
            'Use this when a single variable, single expression, single control property, or similiar object
            'is to be evaluated and different actions are taken depending on the value of the expression
            'Advantage is it is easier to read than multiple if statements
            Select Case grossPayDecimal
                Case Is <= TAX_08_PERCENT
                    federalTaxDecimal = Decimal.Round(TAX_08_LEVEL * grossPayDecimal, 2)
                Case Is <= TAX_18_PERCENT
                    federalTaxDecimal = Decimal.Round(grossPayDecimal * TAX_18_LEVEL, 2)
                Case Else
                    federalTaxDecimal = Decimal.Round(grossPayDecimal * TAX_28_LEVEL, 2)
            End Select
            'Return a value
            Return federalTaxDecimal
        End Function
        'This functions anccepts an input parameter grossPayDecimal that is passed to the function ffrom the calling procedure
        Private Function computeBenefitsDeduction(ByVal grossPayDecimal As Decimal) As Decimal
            'CONST VARIABLES NEVER CHANGE ----- REMEMBER THIS!!!!!!!!!!!!!!!!!!!!!!
            'Const for Insurance - These are not module level variables because the results can change after being added together
            Const MEDICAL_INSURANCE As Decimal = 35.75D
            Const LIFE_INSURANCE As Decimal = 18.35D
            Const DENTAL_INSURANCE As Decimal = 4D
            'Declare variables used within the function
            Dim benefitsCostDecimal As Decimal
            'Compute insurance benefit deduction
            If medicalInsuranceCheckBox.Checked Then
                benefitsCostDecimal += MEDICAL_INSURANCE
            End If
            If lifeInsuranceCheckBox.Checked Then
                benefitsCostDecimal += LIFE_INSURANCE
            End If
            If dentalInsuranceCheckBox.Checked Then
                benefitsCostDecimal += DENTAL_INSURANCE
            End If
            'Use the retirement rate set in the checked changed event for the retirement radio button controls
            benefitsCostDecimal += Decimal.Round(grossPayDecimal * retirementRateDecimal, 2)
            'Return cost of benefits
            Return benefitsCostDecimal
        End Function
        Private Function validData() As Boolean
            'The validData function shown here is an example of a Boolean function because it returns a value of either True
            'or False - the business rules are either all satisfied or the data cannot be processed
            'Assume the data is not valid
            validData = False
            'Enforce data validation rules
            If nameTextBox.Text = String.Empty Then
                'Required employee name is missing
                MessageBox.Show("Name is required", "Name Missing Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf employeeIDMaskedTextBox.Text.Trim.Length <> 11 OrElse employeeIDMaskedTextBox.Text.IndexOf(" ", 0, employeeIDMaskedTextBox.Text.Length) <> -1 Then
                'Required employee ID is not long enough or is not complete
                MessageBox.Show("Employee ID is not complete", "Employee ID Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf departmentTextBox.Text = String.Empty Then
                'Required department is missing
                MessageBox.Show("Department is required", "Department Missing Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf IsNumeric(hoursWorkedTextBox.Text) = False OrElse (Decimal.Parse(hoursWorkedTextBox.Text, Globalization.NumberStyles.Number) <= 0D Or Decimal.Parse(hoursWorkedTextBox.Text, Globalization.NumberStyles.Number) > 60D) Then
                'Hours must be numeric and within allowable range
                MessageBox.Show("Hours worked must be a number between 0 and 60", "Hours Value Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf IsNumeric(payRateTextBox.Text) = False OrElse Decimal.Parse(payRateTextBox.Text, Globalization.NumberStyles.Currency) <= 0D Then
                'Pay rate must be numeric and greater than zero
                MessageBox.Show("Pay rate worked must be a number and greater than zero.", "Pay Rate Value Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                'Data rules are all valid
                validData = True
            End If
        End Function
    #End Region
    End Class
    I have to cut my code down so it is missing the top portion...please let me know if I need to paste it in

  2. #2
    Join Date
    Sep 2000

    Re: exit on menu

        Handles ExitToolStripMenuItem.Click
    You have two functions that Handle this event. So each of them runs their own set of code.

    In this case, both your

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click, ExitToolStripMenuItem.Click,
    functions run. I would say remove the following Function altogether.

     Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
            'Call exit button click event
        End Sub
    Last edited by sotoasty; December 7th, 2012 at 12:38 PM. Reason: code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.