Public Group

This topic is 4227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Trying to make a rule when the user enters information the first time in the current value, that info is set into the previous value. Thus would be added to number of gallons and then calculate for the charge The next step I need to do, is when the user enters another number into the current value that it is greater than the previous value. But if it is lower I need to make a message appeear. I need to use a try/catch block somewhere in the program. I know this is alot to ask but I have been trying for days to figure it out and I am completely stumped please help. Option Explicit On Option Strict Off --------------------------------------------------------------------------- Private Sub uiExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiExitButton.Click 'ends the application Me.Close() End Sub ---------------------------------------------------------------------------- Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click Dim CurrentValue As Double Dim PreviousValue As Double 'PreviousValue = Double.Parse(PreviousTextBox.Text) Try 'enter/display value for current gallons CurrentValue = Double.Parse(CurrentTextBox.Text) GalUsedLabel.Text = CurrentValue PreviousTextBox.Text = GalUsedLabel.Text Catch ex As Exception 'When Current Value is Less than Previous Value 'display message , clear & focus text box MessageBox.Show(CurrentValue, "Average", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Try If PreviousTextBox.Text < CurrentTextBox.Text Then MsgBox("Try Again", MsgBoxstyle.Information, "Invalid Input") End If 'Clear Current Text Box & Grab Focus CurrentTextBox.Text = "" CurrentTextBox.Focus() 'calculate charge ChargeLabel.Text = Format(GalUsedLabel.Text * 1.75, "Currency") End Sub ---------------------------------------------------------------------------- Private Sub CurrentTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CurrentTextBox.KeyPress 'allow only numbers If e.KeyChar < "0" OrElse e.KeyChar > "9" Then e.Handled = True End If End Sub ----------------------------------------------------------------------------- Private Sub PreviousTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles PreviousTextBox.KeyPress 'allow only numbers If e.KeyChar < "0" OrElse e.KeyChar > "9" Then e.Handled = True End If End Sub End Class

Share on other sites
Where to begin? I don't think you understand the usage of exceptions as your comments in the catch block indicate you expect the code to run every time. Exceptions are to catch errors, and while it is admirable to use exceptions, they aren't really meant to be used like you have them. Like not being able to convert a string to a number(the most likely exception in your code).

try
// do stuff
CurrentValue = Double.Parse(CurrentTextBox.Text)
// do stuff
catch InvalidCastException ' not sure on actual exception name
MessageBox("enter a number next time!")
end try

Another thing you seem to be confused on is the difference between text and integers. once you convert a text to an integer, you should generally keep it in that form.

Now time to hit you with some psuedo-code:

Say you have two integers, cur_val and prev_val
Assign cur_val to prev_val
Get a number from the user, assign it cur_val
Find out which value is greater
If prev_val is less than cur_val, yell at the user

Alternatively, you can write your own exception and then throw it. I never did that when I used VB.net so I can't tell you how.

try
Say you have two integers, cur_val and prev_val
Assign cur_val to prev_val
Get a number from the user, assign it cur_val
Find out which value is greater
If prev_val is less than cur_val, throw BadValueException
catch InvalidCastException
Messagebox("Enter in a NUMBER, dumbo!"
Messagebox("Make the new value less than the old value!"
end try

Option Explicit On Option Strict Off'--------------------------------------------------------------------------- Private Sub uiExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiExitButton.Click        'ends the application        Me.Close()    End Sub'----------------------------------------------------------------------------    Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click        Dim CurrentValue As Double        Dim PreviousValue As Double               'PreviousValue = Double.Parse(PreviousTextBox.Text)        Try            'enter/display value for current gallons            CurrentValue = Double.Parse(CurrentTextBox.Text)            GalUsedLabel.Text = CurrentValue            PreviousTextBox.Text = GalUsedLabel.Text        Catch ex As Exception            'When Current Value is Less than Previous Value            'display message , clear & focus text box            MessageBox.Show(CurrentValue, "Average", _             MessageBoxButtons.OK, MessageBoxIcon.Information)        End Try        If PreviousTextBox.Text < CurrentTextBox.Text Then            MsgBox("Try Again", MsgBoxstyle.Information, "Invalid Input")        End If        'Clear Current Text Box & Grab Focus        CurrentTextBox.Text = ""        CurrentTextBox.Focus()        'calculate charge        ChargeLabel.Text = Format(GalUsedLabel.Text * 1.75, "Currency")    End Sub'----------------------------------------------------------------------------    Private Sub CurrentTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CurrentTextBox.KeyPress        'allow only numbers        If e.KeyChar < "0" OrElse e.KeyChar > "9" Then            e.Handled = True        End If    End Sub'-----------------------------------------------------------------------------    Private Sub PreviousTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles PreviousTextBox.KeyPress        'allow only numbers        If e.KeyChar < "0" OrElse e.KeyChar > "9" Then            e.Handled = True        End If    End SubEnd Class

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 14
• 9
• 9
• 9
• Forum Statistics

• Total Topics
632919
• Total Posts
3009208
• Who's Online (See full list)

There are no registered users currently online

×