I'm searching for this, because I imagine it's really easy, but I must be using bad terminology because I'm not finding anything useful...
I want to make sure that a text box has numbers entered into it - if not numbers entered, I want it to display an error message and *STOP the calculation* - right now, if I click the calculate button and there is text in the field, it displays the appropriate message but it still calculates. I feel like it needs an 'else' before the calculation to actually segregate it, but if I do 'else' it out, then it won't calculate because the previous 'if' says to proceed if the field isn't blank...
Private Sub CalculateButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CalculateButton.Click
' Create memory space for numbers in "Group Rental Information" group box
Dim RentalChargeDecimal As Decimal
Dim MinutesInteger As Integer
' Check for presence of a group name
If GroupTextBox.Text = "" Then
MessageBox.Show("Please enter a group name", "Missing Group Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
GroupTextBox.Focus()
' Check for appropriate information being entered
ElseIf MinutesTextBox.Text = "" Then
' Display error message
MessageBox.Show("Please enter the number of minutes", "Missing Data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
MinutesTextBox.Focus()
' Check for a number being entered
ElseIf MinutesTextBox.Text <> "" Then
Try
'Enter number of minutes into memory for calculation
MinutesInteger = Integer.Parse(MinutesTextBox.Text)
Catch ex As Exception
' Display error message
MessageBox.Show("The number of minutes must be numeric", "Non-numeric Data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
MinutesTextBox.Focus()
End Try
' Execute calculation
' Calculate rental cost - Rate is $200/hr.
RentalChargeDecimal = CDec((MinutesInteger / 60 * 200))
RentalChargeLabel.Text = RentalChargeDecimal.ToString("C")
' Add new rental cost to rental totals and display
RentalsTotalDecimal += RentalChargeDecimal
RentalsTotalLabel.Text = RentalsTotalDecimal.ToString("C")
' Add new group to group totals and display
GroupsTotalInteger += 1
GroupsTotalLabel.Text = GroupsTotalInteger.ToString
' Update average charge per group and display
RunningAverageDecimal = (RentalsTotalDecimal / GroupsTotalInteger)
RunningAverageLabel.Text = RunningAverageDecimal.ToString("C")
End If
End Sub
what I want to do is replace the <> "" part of that last ElseIf to something like "ElseIf MinutesTextBox.Text IsNumeric" but I don't know how to make that work (it's also ahead of our class, so I may get points off if I do it, but if there's anything I can do to make this work it'll be worth a few points)
I want to make sure that a text box has numbers entered into it - if not numbers entered, I want it to display an error message and *STOP the calculation* - right now, if I click the calculate button and there is text in the field, it displays the appropriate message but it still calculates. I feel like it needs an 'else' before the calculation to actually segregate it, but if I do 'else' it out, then it won't calculate because the previous 'if' says to proceed if the field isn't blank...
Private Sub CalculateButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CalculateButton.Click
' Create memory space for numbers in "Group Rental Information" group box
Dim RentalChargeDecimal As Decimal
Dim MinutesInteger As Integer
' Check for presence of a group name
If GroupTextBox.Text = "" Then
MessageBox.Show("Please enter a group name", "Missing Group Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
GroupTextBox.Focus()
' Check for appropriate information being entered
ElseIf MinutesTextBox.Text = "" Then
' Display error message
MessageBox.Show("Please enter the number of minutes", "Missing Data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
MinutesTextBox.Focus()
' Check for a number being entered
ElseIf MinutesTextBox.Text <> "" Then
Try
'Enter number of minutes into memory for calculation
MinutesInteger = Integer.Parse(MinutesTextBox.Text)
Catch ex As Exception
' Display error message
MessageBox.Show("The number of minutes must be numeric", "Non-numeric Data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
MinutesTextBox.Focus()
End Try
' Execute calculation
' Calculate rental cost - Rate is $200/hr.
RentalChargeDecimal = CDec((MinutesInteger / 60 * 200))
RentalChargeLabel.Text = RentalChargeDecimal.ToString("C")
' Add new rental cost to rental totals and display
RentalsTotalDecimal += RentalChargeDecimal
RentalsTotalLabel.Text = RentalsTotalDecimal.ToString("C")
' Add new group to group totals and display
GroupsTotalInteger += 1
GroupsTotalLabel.Text = GroupsTotalInteger.ToString
' Update average charge per group and display
RunningAverageDecimal = (RentalsTotalDecimal / GroupsTotalInteger)
RunningAverageLabel.Text = RunningAverageDecimal.ToString("C")
End If
End Sub
what I want to do is replace the <> "" part of that last ElseIf to something like "ElseIf MinutesTextBox.Text IsNumeric" but I don't know how to make that work (it's also ahead of our class, so I may get points off if I do it, but if there's anything I can do to make this work it'll be worth a few points)