|
-
Feb 24th, 2008, 03:31 AM
#1
Thread Starter
Junior Member
divide by zero while entering a numeric value that begins with 0
basically I have a textbox
Code:
Private Sub txt_NumberA_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_NumberA.TextChanged
Try
NumberA = Decimal.Parse(txt_NumberA.Text)
Catch ex As Exception
MsgBox("This program requires a numeric input. Please try again. Enter .500 as 0.500.")
Return
End Try
NumberB = 5.0
NumberC = NumberB/NumberA
lbl_NumberC.Text = NumberC
End Sub
I have no problem entering any number that does not start with a zero, however, if I want to enter 0.5 in, then as I type in '0', then I have division by zero and the program crashes.
Could you please help me to fix this? Many thanks,
Nina
Last edited by nina_le_huynh; Feb 24th, 2008 at 04:21 AM.
-
Feb 24th, 2008, 04:22 AM
#2
Frenzied Member
Re: divide by zero while entering a numeric value that begins with 0
Try this:
Code:
Private Sub txt_NumberA_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_NumberA.TextChanged
If Val(txt_NumberA.Text) <> 0 Then
Try
numbera = Decimal.Parse(txt_NumberA.Text)
Catch ex As Exception
MsgBox("This program requires a numeric input. Please try again. Enter .500 as 0.500.")
Return
End Try
numberb = 5.0
numberc = numberb / numbera
lbl_NumberC.Text = numberc
End If
End Sub
-
Feb 24th, 2008, 04:36 AM
#3
Thread Starter
Junior Member
Re: divide by zero while entering a numeric value that begins with 0
03myersd,
It worked, thank you very much.
Nina
 Originally Posted by 03myersd
Try this:
Code:
Private Sub txt_NumberA_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_NumberA.TextChanged
If Val(txt_NumberA.Text) <> 0 Then
Try
numbera = Decimal.Parse(txt_NumberA.Text)
Catch ex As Exception
MsgBox("This program requires a numeric input. Please try again. Enter .500 as 0.500.")
Return
End Try
numberb = 5.0
numberc = numberb / numbera
lbl_NumberC.Text = numberc
End If
End Sub
-
Feb 24th, 2008, 07:14 AM
#4
Re: divide by zero while entering a numeric value that begins with 0
If the user is going to type in 53 your code displays in lbl_NumberC.Text the following:
1.0
.09
Is that what you wanted or did you just want the .09? If so you are in the wrong event handler, and also why you had the problem with 0.
-
Feb 24th, 2008, 01:20 PM
#5
Re: divide by zero while entering a numeric value that begins with 0
I'd say you are in the wrong event handler, too. Re-doing the calculation for every character being entered means lots of extra work. LostFocus might be a better place.
However, this code will be faster. Catching an exception is a painfully slow process (though there is no cost if no exception is thrown), so using TryParse will produce better average results than dealing with an exception.
vb Code:
Private Sub txt_NumberA_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_NumberA.TextChanged
if not Decimal.TryParse(txt_NumberA.Text,NumberA)
MsgBox("This program requires a numeric input. Please try again. Enter .500 as 0.500.")
Return
End if
NumberB = 5.0
if NumberA<>0 then
NumberC = NumberB/NumberA
else
NumberC=0
end if
lbl_NumberC.Text = NumberC.ToString
End Sub
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|