Jump to content
  • Advertisement
Sign in to follow this  
gamensane

vb.net help please help

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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!"
catch BadValueException
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 Sub
End Class

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!