menu button perform click
I am learning to create menu buttons and use the perform click. When I began by using the about in my code it works fine. However, when I try to do a perform click wiht my exit button in the menu strip it does not do anything...
Code:
Public Class Form1
'Module level variables - These are private because they do
'Const for Retirement
Const RETIREMENT_STANDARD As Decimal = 0.05D
Const RETIREMENT_401A As Decimal = 0.08D
Private retirementRateDecimal As Decimal
Private Sub computeButton_Click(sender As Object, e As EventArgs) Handles computeButton.Click
Try
'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
'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
Dim hoursDecimal, payRateDecimal, grossPayDecimal, benefitsDecimal, netPayDecimal, federalTaxDecimal As Decimal
'Make sure textboxes are valid
If nameTextBox.Text.Trim = String.Empty Then
MessageBox.Show("Name is required", "Name Missing Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
nameTextBox.Focus()
nameTextBox.SelectAll()
ElseIf employeeIDMaskedTextBox.MaskCompleted = False Then
MessageBox.Show("Employee ID is not complete", "Employee ID Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
employeeIDMaskedTextBox.Focus()
employeeIDMaskedTextBox.SelectAll()
ElseIf departmentTextBox.Text.Trim = String.Empty Then
MessageBox.Show("Department is required", "Department Missing Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
departmentTextBox.Focus()
departmentTextBox.SelectAll()
'This means if the result in this textbox is not a number or is less than 0 or greater than 60 to return an error
ElseIf IsNumeric(hoursWorkedTextBox.Text) = False Or Decimal.Parse(hoursWorkedTextBox.Text,
Globalization.NumberStyles.Number) <= 0D Or Decimal.Parse(hoursWorkedTextBox.Text, Globalization.NumberStyles.Number) > 60D Then
MessageBox.Show("Hours worked must be a number between 0 and 60", "Hours Value Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
hoursWorkedTextBox.Focus()
hoursWorkedTextBox.SelectAll()
'This means is the result in this textbox is not a number or less than 0 to return an error
ElseIf IsNumeric(payRateTextBox.Text) = False Or Decimal.Parse(payRateTextBox.Text, Globalization.NumberStyles.Currency) <= 0D Then
MessageBox.Show("Pay rate worked must be a number greater than zero", "Pay Rate Value Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
payRateTextBox.Focus()
payRateTextBox.SelectAll()
Else
'Data rules are valid - Parse the textboxes
hoursDecimal = Decimal.Parse(hoursWorkedTextBox.Text, Globalization.NumberStyles.Number)
payRateDecimal = Decimal.Parse(payRateTextBox.Text, Globalization.NumberStyles.Currency)
'Compute gross pay
If hoursDecimal <= 40D Then
grossPayDecimal = Decimal.Round(hoursDecimal * payRateDecimal, 2)
Else
grossPayDecimal = Decimal.Round((40D * payRateDecimal) + ((hoursDecimal - 40D) * payRateDecimal * 1.5D), 2)
End If
'Compute insurance benefit deduction
If medicalInsuranceCheckBox.Checked Then
benefitsDecimal += MEDICAL_INSURANCE
End If
If lifeInsuranceCheckBox.Checked Then
benefitsDecimal += LIFE_INSURANCE
End If
If dentalInsuranceCheckBox.Checked Then
benefitsDecimal += DENTAL_INSURANCE
End If
'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
'Use the retirement rate set in the checked changed event for the retirement radio button controls
benefitsDecimal += Decimal.Round(grossPayDecimal * retirementRateDecimal, 2)
'Compute the net pay
netPayDecimal = grossPayDecimal - federalTaxDecimal - benefitsDecimal
'Display output
payRateTextBox.Text = payRateDecimal.ToString("C")
grossPayTextBox.Text = grossPayDecimal.ToString("C")
netPayTextBox.Text = netPayDecimal.ToString("C")
federalTaxTextBox.Text = federalTaxDecimal.ToString("N")
benefitsTextBox.Text = benefitsDecimal.ToString("N")
End If
Catch ex As Exception
MessageBox.Show("Unexpected error: " & ControlChars.NewLine & ex.Message, "Compute Button Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.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,
MessageBoxDefaultButton.Button2)
If buttonDialogResult = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
End Sub
Private Sub resetButton_Click(sender As Object, e As EventArgs) Handles resetButton.Click
'Reset all textboxcontrol
nameTextBox.Clear()
employeeIDMaskedTextBox.Clear()
departmentTextBox.Clear()
hoursWorkedTextBox.Clear()
payRateTextBox.Clear()
grossPayTextBox.Clear()
federalTaxTextBox.Clear()
benefitsTextBox.Clear()
netPayTextBox.Clear()
'Reset retirement benefit status to none
noRetirementPlanRadioButton.Checked = True
'Uncheck benefits checkboxes
medicalInsuranceCheckBox.Checked = False
lifeInsuranceCheckBox.Checked = False
dentalInsuranceCheckBox.Checked = False
'Set focus to name textbox
nameTextBox.Focus()
End Sub
Private Sub noneRetirementPlanRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles noRetirementPlanRadioButton.CheckedChanged,
retirementStandardRadioButton.CheckedChanged, retirement401ARadioButton.CheckedChanged
'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 FileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FileToolStripMenuItem.Click
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
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
exitButton.PerformClick()
End Sub
End Class
Re: menu button perform click
Are you sure your Exit menu item works on its own? Might be a stupid question, but that is 99% the case :)
Have you tried doing something like this :
Code:
exitButton_Click(sender, e)
instead of PerformClick?
Re: menu button perform click
I tried me.close() and it also does not do anything
Re: menu button perform click
Quote:
Originally Posted by
bkruep
I tried me.close() and it also does not do anything
Then there is something wrong with your ExitToolStripMenuItem_Click event. Try doubleclicking on it in design view, and add the code again. If that doesn't work, zip your project ( without the .exe files ) and post it here.