Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculator needs to "see" wrong input

Status
Not open for further replies.

ph03nix

Technical User
Jul 2, 2005
2
IN
Hello,

I'm making a calculator with four buttons (*, /, - and +). pressing one of the four buttons should calculate the desired function from two Textboxes and put it in a third one. The problem is that it has to give an error if one of the two inputs is not a number.... i just don't know how to program this.. please help, i'm just a beginner

thanx in advance
 
You might try the following:

Sub cmdButton_Click

if (isnumeric(editbox1.text)) then
if (isnumeric(editbox2.text)) then
PerformCalculation
else
msgbox "Operand 2 is not Numeric"
end if
else
msgbox "Operand 1 is not Numeric"
endif

End Sub


IsNumeric is a built-in function of VB.

Good Luck
 
Just be forewarned that some text is recognized as numeric. This should not affect your calculations though.
For example:
IsNumeric("3d4") returns True
 
dsi is correct. A simple workaround for that is the following code:

if trim(str(val(editbox.text))) = trim(editbox.text) then
it only contains numeric characters
else
its invalid
end if

Good Luck
 
This may be simplistic but you can also limit the input to your boxes with this. It would not allow them to enter anything but numbers.

Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
Beep
(enter the numbers into your variables here)
End If

You can insert appropriate messages if you want but with the beeps they eventually get the idea. :) And you would have to do this for every textbox.




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top