-
Need some insight
I am working on a task given by my instructor who has stated that his skills are not in programming at all but he agree to teach the class. The assignment itself is very basic and I am trying to follow the directions given in the book due to the fact the instructor does not state exactly what he wants. The issue I need insight with is in the btnCalculate where I am trying to code an event handler that can deal with the off chance that a user could click the Calculate button without any data entered. I run into a wall (InvalidCastException) with getting it to allow the user to enter data. I have tried While, Do until loops and decided the if statement would work. This occurs only after the messagebox is cleared. Here is the code:
Code:
Public Class LongDistanceCall
Dim notANumber As Boolean
Dim DAYRATE As Double = 0.07 ' long distance day time calling plan rate per minute
Dim EVENRATE As Double = 0.12 ' Long distance evening calling plan rate per minute
Dim OFFPEAK As Double = 0.05 ' long distance off peak hours calling plan rate per minute
Dim MinutesUsed As Double
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
' ********************************************************************************************* problem area
' Vaildates the value of the number enter is higher then 0
If (txtMinutesUsed.Text) = "" Then
MessageBox.Show("No data? Really?? Come on, I need something here to work with please.", "ERROR DETECTED", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
MinutesUsed = CDbl(txtMinutesUsed.Text) ' converts the value entered into a double of computation
' ********************************************************************************************* problem area
If (MinutesUsed) <= 0 Then
MessageBox.Show("Can not less then 1 Minute", "ERROR DETECTED", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtMinutesUsed.Text = String.Empty
End If
' Performs the calculation based on which option is picked and displays a error in the event no option is selected
If RadDayTime.Checked = True Then
lblCharges.Text = FormatCurrency(MinutesUsed * DAYRATE)
ElseIf RadEvening.Checked = True Then
lblCharges.Text = FormatCurrency(MinutesUsed * EVENRATE)
ElseIf RadOffPeak.Checked = True Then
lblCharges.Text = FormatCurrency(MinutesUsed * OFFPEAK)
Else
MessageBox.Show("Please Select one of the offered plans", "Missing information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
' Resets the values on clear or empty to prevent data overlaying
lblCharges.Text = String.Empty
RadDayTime.Checked = False
RadEvening.Checked = False
RadOffPeak.Checked = False
txtMinutesUsed.Clear()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
' closes the form and clears the memory
Me.Close()
End
End Sub
Private Sub txtMinutesUsed_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMinutesUsed.KeyDown
' ensures only numbers from keypad or keys 2 thru 11 on row two work
notANumber = False
If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
If e.KeyCode <> Keys.Back Then
notANumber = True
End If
End If
End If
If Control.ModifierKeys = Keys.Shift Then
notANumber = True
End If
End Sub
Private Sub txtMinutesUsed_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMinutesUsed.KeyPress
' turns off non-numeric keys
If notANumber = True Then
e.Handled = True
End If
End Sub
End Class
Thank you for your time and input.
-
Re: Need some insight
Chit Chat is really the wrong place to post this unless you want an irrelevant discussion about politics, religion, IPhones or oceanic predators. Your post really belongs in the VB.Net forum so I've reported it to a mod to get it moved.
I believe your problem is that you're detecting that the user has entered a blank in txtMinutesUsed and then trying to convert it to a double anyway. You need to exit your sub if txtMinutesUsed is blank.
Orcas and Blue Whales FTW!
-
Re: Need some insight
Welcome to the forums DSUstudent59 :wave:
I have moved your question to VB.Net from Chit Chat. Here you will get appropriate responses.
@FunkyDexter: Thanks for the report. :thumb:
-
Re: Need some insight
FD has the problem. You need a Return statement right after the messagebox. Once the MessageBox has gone away, the next line will be the Return, which will exit the sub. The way you have it now, the next line will be the conversion, which can't happen because the textbox is empty.
By the way, while the solution you have will catch empty strings, it is not all that robust. If somebody enters "Blue", the box won't be empty and the conversion will still fail. Rather than checking for empty, take a look in MSDN (or search on here, if the search feature is working, which it may not be yet) for Double.TryParse, which is really what you want to be using in this situation.