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.




Reply With Quote
