473,320 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

need help with VB 08 Calculator

18
I need to create a calculator using combo boxes/procedures/functions etc... and some other stuff. most of it works properly, but, i need to create a user defined function that checks if both input numbers of the calculation are in fact numbers (isnumeric). the function must check both numbers, display a message box and return a value. but it crashes every time i test it - like putting in a letter instead of a number. any help would be great,
here's my code so far: (first function is the one malfunctioning)
Public Class form1
Function checknumber(ByVal num1 As String, ByVal num2 As String) As Boolean
If Not IsNumeric(num1) Then
MsgBox("Input must be a number.")
ElseIf Not IsNumeric(num2) Then
MsgBox("Input must be a number.")
Return False
End If

End Function
Sub rounding(ByVal roundnum As Double)
If txtRound.Enabled = True Then
txtResult.Text = FormatNumber(txtResult.Text, roundnum)
End If
End Sub
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
Dim num1, num2, roundnum As Double
num1 = txtNum1.Text
num2 = txtNum2.Text
roundnum = txtRound.Text


If cmbOperation.Text = "Addition" Then
'calculate result
addition(num1, num2)
End If

If cmbOperation.Text = "Subtraction" Then
'calculate result
subtraction(num1, num2)
End If

If cmbOperation.Text = "Multiplication" Then
'calculate result
multiplication(num1, num2)
End If

If cmbOperation.Text = "Division" Then
'calculate result
division(num1, num2)
End If

If cmbOperation.Text = "Mod" Then
'calculate result
modular(num1, num2)
End If
If cmbOperation.Text = "Exponentiation" Then
'calculate result
exponentiate(num1, num2)
End If

'validate operation
If cmbOperation.Text = "" Then
MsgBox("no operation is selected.")
End If
End Sub


Sub addition(ByVal num1 As Double, ByVal num2 As Double)
checknumber(num1, num2)
txtResult.Text = (num1 + num2)
'build list
lstResults.Items.Add(txtResult.Text)
End Sub

Sub subtraction(ByVal num1 As Double, ByVal num2 As Double)
txtResult.Text = num1 - num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub

Sub multiplication(ByVal num1 As Double, ByVal num2 As Double)
txtResult.Text = num1 * num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub

Sub division(ByVal num1 As Double, ByVal num2 As Integer)
'validate input
If num2 = 0 Then
MsgBox("Number 2 cannot be 0.")
Exit Sub
End If
txtResult.Text = num1 / num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub
Sub modular(ByVal num1 As Double, ByVal num2 As Double)
'validate input
If num2 = 0 Then
MsgBox("Number 2 cannot be 0.")
Exit Sub
End If
txtResult.Text = num1 Mod num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub
Sub exponentiate(ByVal num1 As Double, ByVal num2 As Double)
'validate input
If num1 = 0 And num2 < 0 Then
MsgBox("input is invalid.")
Exit Sub
End If
txtResult.Text = num1 ^ num2
'build list
lstResults.Items.Add(txtResult.Text)
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
lstResults.Items.Clear()
txtNum1.Text = ""
txtNum2.Text = ""
txtRound.Text = ""
txtResult.Text = ""
cmbOperation.Text = ""
radRound.Checked = False
End Sub

Private Sub radRound_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radRound.CheckedChanged
'disable rounding option unless user wants to round
If radRound.Checked = True Then
'allow user to round
txtRound.Enabled = True
Else
'do not allow rounding
txtRound.Enabled = False
End If
End Sub

Private Sub lstResults_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstResults.SelectedIndexChanged
If lstResults.SelectedIndex >= 0 Then
If MsgBox("Use this number in the calculation?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
txtNum1.Text = lstResults.Text
End If
End If
If lstResults.SelectedIndex = -1 Then
MsgBox("no number is selected.")
Exit Sub
End If
End Sub

End Class
Oct 10 '08 #1
8 2754
'--
Try removing the "Return False" code line.
'--
Oct 10 '08 #2
jac130
18
'--
Try removing the "Return False" code line.
'--
don't functions have to return a value? besides, one of the requirements is that i return a value indicating there was a problem
Oct 11 '08 #3
jg007
283 100+
the code that you have for checking the number works fine for me so I am not sure what you are doing wrong, what is the actual error message?

when using it myself I have just created a textbox and a button and used the same textbox text for both values

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Class Form1
  3.  
  4.     Function checknumber(ByVal num1 As String, ByVal num2 As String) As Boolean
  5.         If Not IsNumeric(num1) Then
  6.             MsgBox("Input must be a number.")
  7.         ElseIf Not IsNumeric(num2) Then
  8.             MsgBox("Input must be a number.")
  9.             Return False
  10.         End If
  11.     End Function
  12.  
  13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  14.         checknumber(TextBox1.Text, TextBox1.Text)
  15.  
  16.     End Sub
  17.  
  18. End Class
  19.  
  20.  
you might also want to try catching the values as they are enterend using something like this -

Expand|Select|Wrap|Line Numbers
  1.  Public Sub checknumber(ByVal sender As System.Object, ByVal key As KeyPressEventArgs) Handles TextBox1.KeyPress
  2.  
  3.         If Not IsNumeric(key.KeyChar) Then
  4.             MsgBox("Input must be a number.")
  5.             key.Handled = True
  6.         End If
  7.  
  8.     End Sub
  9.  
  10.  
Oct 11 '08 #4
jac130
18
the code that you have for checking the number works fine for me so I am not sure what you are doing wrong, what is the actual error message?

when using it myself I have just created a textbox and a button and used the same textbox text for both values

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Class Form1
  3.  
  4.     Function checknumber(ByVal num1 As String, ByVal num2 As String) As Boolean
  5.         If Not IsNumeric(num1) Then
  6.             MsgBox("Input must be a number.")
  7.         ElseIf Not IsNumeric(num2) Then
  8.             MsgBox("Input must be a number.")
  9.             Return False
  10.         End If
  11.     End Function
  12.  
  13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  14.         checknumber(TextBox1.Text, TextBox1.Text)
  15.  
  16.     End Sub
  17.  
  18. End Class
  19.  
  20.  
you might also want to try catching the values as they are enterend using something like this -

Expand|Select|Wrap|Line Numbers
  1.  Public Sub checknumber(ByVal sender As System.Object, ByVal key As KeyPressEventArgs) Handles TextBox1.KeyPress
  2.  
  3.         If Not IsNumeric(key.KeyChar) Then
  4.             MsgBox("Input must be a number.")
  5.             key.Handled = True
  6.         End If
  7.  
  8.     End Sub
  9.  
  10.  

i dont get an error message, the program just crashes and highlights the code : num1=txtnum1.text, or which ever text box i put the letter into. this is my function:
public Class Form1
Function checknumber(ByVal num1 As String, ByVal num2 As String) As Boolean
If Not IsNumeric(num1) Then
MsgBox("Input must be a number.")
ElseIf Not IsNumeric(num2) Then
MsgBox("Input must be a number.")
Return False
End If
End Function

and this is how i called it in the addition procedure:

Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
Dim num1, num2 As Double
num1 = txtNum1.Text
num2 = txtNum2.Text

If cmbOperation.Text = "Addition" Then
checknumber(num1, num2)
'calculate result
addition(num1, num2)
Oct 11 '08 #5
jg007
283 100+
this might sound stupid but are you sure you are using the correct textbox name etc and you haven't put a break or something there?

also the other thought as I am writing this is that if you are trying to assign a string to an integer you will get errors and you would need to make sure that num1 or whatever is set as a string, alternatively check if it is numeric before trying to assign it to an integer :)
Oct 11 '08 #6
jac130
18
this might sound stupid but are you sure you are using the correct textbox name etc and you haven't put a break or something there?

also the other thought as I am writing this is that if you are trying to assign a string to an integer you will get errors and you would need to make sure that num1 or whatever is set as a string, alternatively check if it is numeric before trying to assign it to an integer :)
yeah, the txtbox names are all correct. Every other part of the calculator works properly. maybe i'm not calling it correctly, and i have changed the parameters to "as integers" , but still it crashes and hightlights the code of which ever txtbox i put the letter into. and says "conversion from string "e" to type double is not valid"

here's the function now, and how i've called it:


Function checknumber(ByVal num1 As Integer, ByVal num2 As Integer) As Boolean
If Not IsNumeric(num1) Then
MsgBox("Input must be a number.")
ElseIf Not IsNumeric(num2) Then
MsgBox("Input must be a number.")
Return False
End If
End Function

Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
Dim num1, num2 As Double
num1 = txtNum1.Text
num2 = txtNum2.Text

If cmbOperation.Text = "Addition" Then
If Not checknumber(num1, num2) Then
Exit Sub
End If
'calculate result
addition(num1, num2)
Oct 11 '08 #7
jg007
283 100+
not being funny but there is your problem, you cannot convert a character to a double value!, it you read the message it will explain what is going wrong!

you are trying to convert what is a string value ( example 'A' ) to a double which cannot be done so you need to check if it is numeric BEFORE assigning it!

example code -

Expand|Select|Wrap|Line Numbers
  1.  
  2. if isnumb ( numb1 , numb2 )  then 
  3. num1 = textbox1.text
  4. num2 = texbox2.text
  5. ' your code goes here
  6. else 
  7. msgbox ( "Please enter numbers only!")
  8. end if
  9.  
  10.  
  11.  
  12.  
and the function -

Expand|Select|Wrap|Line Numbers
  1.  
  2. function isnumb ( byval num1 as string , byval numb2 as integer )
  3.  
  4. dim numericyes as boolean = false
  5.  
  6. if isnumeric(num1) and isnumeric(num2) then 
  7. numericyes = true
  8. return numericyes
  9. else
  10. return  numericyes
  11. end if
  12.  
  13. end function 
  14.  
  15.  
sorry I an being too lazy to load vb to confirm it but this code should be roughly correct :)
Oct 11 '08 #8
NeoPa
32,556 Expert Mod 16PB
This thread is not only double-posted, it is also a homework question without [ CODE ] tags. A hat-trick of offenses.

I would ask all responders to remember that our rules prohibit such questions and that direct responses (simply providing answers) are not allowed either. Please see his other thread (VB 08 Calculator) for an example of how help should be provided to such questions when, and only when, the OP conforms to the correct format for asking for help in these circumstances.

Jac130, This is an official site warning. As both offenses occured before you received the first warning it will be treated as a single offense. You should be aware though, that subsequent offenses will result in a temporary ban of your account, followed by a permanent ban if further offenses occur after that.

Administrator.
Oct 12 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anon Email | last post by:
Hi people, In the following code, in the main method, why is it possible to pass "input" as an argument to the method TheCalculator.Execute()? This method is supposed to accept references to...
4
by: Kim14 | last post by:
I have a table that works fine in IE, but doesn't work in Netscape or Firefox. It should automatically come up with numbers in some of the fields and depending what is entered, it should calculate...
3
by: Sean McCourt | last post by:
Hi I am doing a JavaScript course and learning from the recommed book (JavaScript 3rd Edition by Don Gosslin) Below is one of the exercises from the book. I get this error message when I try to...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
3
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
2
by: masker | last post by:
I was on the web trying to find a javascript that would allow me to input a number on my website and have it increase by a given percentage every second. During my search I found the Earth...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
17
by: Max347 | last post by:
This is my first post, so hopefully I can give enough information. I am running windows xp, using jGrasp to write code. I need to make a calculator in which the user inputs 2 floating point numbers...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.