i have to make a calculator in visual basic, it's slightly more complex one, using a combo box/procedures/functions. Anyway, i need to create a user defined function to make sure both input numbers (of the the calculation) are in fact numbers, the isnumeric function. both numbers have to be checked in the same function, and i must return a msgbox (as part of the function) displaying an error and return a value indicating there was a problem. it always crashes when i test it - entering a letter instead of a number into either of the textboxes. here's my code so far: the first function is the function that's malfunctioning. any help would be great - 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
-
-
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
-
Dim num1, num2 As Double
-
num1 = CStr(txtNum1.Text)
-
num2 = CStr(txtNum2.Text)
-
-
If cmbOperation.Text = "Addition" Then
-
checknumber(num1, num2)
-
'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
10 2607 NeoPa 32,556
Expert Mod 16PB Homework Assignment
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.
Administrator.
NeoPa 32,556
Expert Mod 16PB
I have also moved this to the appropriate forum for VB questions. You posted it in the Access / VBA forum.
Homework Assignment
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.
Administrator.
i did attempt it myself, where do you think all that code came from. i only asked about the user defined function validating the input as a number.
NeoPa 32,556
Expert Mod 16PB
I have no argument with what you say, as far as it goes.
If you read my post carefully though (in your position I would have) you will see that it also mentions posting questions regarding any difficulties that you may have come across. All your question stated was that your function wasn't working. There is nowhere any description of the difficulties you encountered. What you attempted to do to resolve your initially observed problem.
As a student (of whatever form) we are not prepared to help sabotage your learning process by allowing you (or anyone in your position) simply to drop their problem on the forum and expect solutions to be provided. If you read our rules in the readily available Help link, you will see why we take this stance with students particularly.
We are more than willing to help, but for you we would rather help you to learn, than simply do it for you.
Now, I am happy to help you with this (as I've already become involved) although I am a VBA man more than VB. I don't imagine this will be too much of a block though. At this level they are very similar I understand. I would expect you to start with explaining exactly what goes wrong, and what, if anything, are your thoughts on why this is happening.
Alternatively you can wait for a more specialist VB expert to help. Either way, you should know we would expect you to drive. We would only be there to give guidance.
the rest of the calculator works, but when i type a letter into either of the textboxes and try to calculate it, the program crashes. I have no idea why this happens. the code looks fine to me. i need it to validate that the inputs are numbers but it just crahses.
NeoPa 32,556
Expert Mod 16PB - 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
Ok, What sort of crash is it. I'm assuming degree level here - is that right?
A shool pupil can possibly get away with just "it crashes."
I need to know WHERE it crashes and what the error message was if there was one. Describe clearly what happens or I can't help you.
PS. I asssume this code has compiled successfully.
can you merge the two threads here please.
** Admin edit - Spoonfeeding help removed **
NeoPa 32,556
Expert Mod 16PB
Sorry to cut your help out there JG. We have to be careful how we handle homework type questions though, and simply feeding answers is not acceptable (although I very much sympathise with - and applaud - your wanting to help).
I have locked the other thread ( need help with VB 08 Calculator) you referred to, as explained in that thread.
NeoPa 32,556
Expert Mod 16PB
I should add that you are, of course, free to offer further assistance, but with the proviso that you extract the solution from the OP themselves, rather than provide it for them.
Possibly direct them where to look, but don't give out answers directly. This doesn't help them (or anyone really) in the long run.
NeoPa 32,556
Expert Mod 16PB
I've just been told that, as this is in VB 08, it must be a .NET question rather than a VB one.
I am moving it to the .NET forum.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: mwh |
last post by:
Hi. If you remember, I posted Expressons Help. Now I am making a
calculator with javascript. I can't get this to work:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
|
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....
...
|
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
|
by: Art |
last post by:
Hi,
In part of my application the user may need to do a simple arithmetic
calculation in order to get the value to put in a text box. I was thinking
that it would be good if I could display the...
|
by: PieMan2004 |
last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :)
Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
|
by: firstcustomer |
last post by:
Hi,
Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that
someone will be able to point me to a ready-made solution to my
problem!
A friend of mine (honest!) is wanting to...
|
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...
|
by: Deathwing |
last post by:
Hi everyone one I'm playing around with trying to make an expense calculator.
I would like it so that the user can keep enter expenses until they have no more expenses. Then I would like for the...
|
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,...
|
by: mandy335 |
last post by:
public class Calculator {
private long input = 0; // current input
private long result = 0; // last input/result
private String lastOperator = ""; // keeps track of...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |