Try/Catch Help

bigboywasim

Member
Joined
Sep 29, 2006
Messages
18
Programming Experience
Beginner
VB.NET:
Dim blnErrorFlag As Boolean
Dim UserError1 As InvalidCastException
Dim UserError2 As ArithmeticException
 
Try
If CDec(txtCarSalesPrice.Text) > 100000 Then
MsgBox("Car Sales Price Exceeds 100000. Enter Smaller Number", MsgBoxStyle.Critical)
txtCarSalesPrice.BackColor = Color.Red
blnErrorFlag = True
End If
If CDec(txtTradeInAllowance.Text) > 50000 Then
MsgBox("Trade In Allowence Exceeds 50000. Enter Smaller Number", MsgBoxStyle.Critical)
txtTradeInAllowance.BackColor = Color.Red
blnErrorFlag = True
End If
Catch UserError1
MsgBox("Enter Only Numbers")
Catch UserError2
MsgBox("Overflow Please Enter Smaller Value")
End Try

I have a problem with two things. The first one is that if I enter a value only in one textbox and hit the calculate button the program crashes. I get exited out of the form. I think it has something to do with the CDec since the complier has nothing to convert. I do not know how to fix this.

The second problem I have is that I do not know how to code to change the background of the two textboxes to red when UserError1 or UserError2 happens. Please someone help me with this.
 
in the catch parts just add the color changing code (you've already got it in the try part, each inside the if/end if):
VB.NET:
Dim blnErrorFlag As Boolean = False
 
Try
  If CDec(txtCarSalesPrice.Text) > 100000 Then
    MsgBox("Car Sales Price Exceeds 100000. Enter Smaller Number", MsgBoxStyle.Critical)
    txtCarSalesPrice.BackColor = Color.Red
    blnErrorFlag = True
  End If
  If CDec(txtTradeInAllowance.Text) > 50000 Then
    MsgBox("Trade In Allowence Exceeds 50000. Enter Smaller Number", MsgBoxStyle.Critical)
    txtTradeInAllowance.BackColor = Color.Red
    blnErrorFlag = True
  End If
Catch UserError1 As InvalidCastException
  txtCarSalesPrice.BackColor = Color.Red
  txtTradeInAllowance.BackColor = Color.Red
  MsgBox("Enter Only Numbers")
Catch UserError2 As ArithmeticException
  txtCarSalesPrice.BackColor = Color.Red
  txtTradeInAllowance.BackColor = Color.Red
  MsgBox("Overflow Please Enter Smaller Value")
End Try


as for using a textbox for only numbers, i would use the NumericUpDown control (it's in the toolbox by default) you can specify the Min and Max (as well as how many decimal places) then to use the value in code, you dont have to convert it from a string, just use the contol's Value() property (which stores it as a decimal)
 
Thanks I got the color part down. Is the program suppose to crash when these Errors occur? I am not allowed to use the UpandDown control. My prgram is still crashing when I put in only one value in my textbox. So if I have nothing in one textbox the program crashes.
 
conversion from nothing to a number does cause it to crash

you could check the see if the textbox is empty, if it is, just put a zero in (or if it's not numeric, put a zero in)

VB.NET:
If txtCarSalesPrice.Text = "" OrElse IsNumeric(txtCarSalesPrice.Text) = False Then txtCarSalesPrice.Text = "0"
 
Back
Top