473,399 Members | 3,919 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,399 software developers and data experts.

VB 08 Calculator

18
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
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Function checknumber(ByVal num1 As String, ByVal num2 As String) As Boolean
  4.         If Not IsNumeric(num1) Then
  5.             MsgBox("Input must be a number.")
  6.         ElseIf Not IsNumeric(num2) Then
  7.             MsgBox("Input must be a number.")
  8.             Return False
  9.         End If
  10.     End Function
  11.  
  12.     Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
  13.         Dim num1, num2 As Double
  14.         num1 = CStr(txtNum1.Text)
  15.         num2 = CStr(txtNum2.Text)
  16.  
  17.         If cmbOperation.Text = "Addition" Then
  18.             checknumber(num1, num2)
  19.             'calculate result
  20.             addition(num1, num2)
  21.         End If
  22.  
  23.         If cmbOperation.Text = "Subtraction" Then
  24.             'calculate result
  25.             subtraction(num1, num2)
  26.         End If
  27.  
  28.         If cmbOperation.Text = "Multiplication" Then
  29.             'calculate result
  30.             multiplication(num1, num2)
  31.         End If
  32.  
  33.         If cmbOperation.Text = "Division" Then
  34.             'calculate result
  35.             division(num1, num2)
  36.         End If
  37.  
  38.         If cmbOperation.Text = "Mod" Then
  39.             'calculate result
  40.             modular(num1, num2)
  41.         End If
  42.         If cmbOperation.Text = "Exponentiation" Then
  43.             'calculate result
  44.             exponentiate(num1, num2)
  45.         End If
  46.  
  47.         'validate operation 
  48.         If cmbOperation.Text = "" Then
  49.             MsgBox("no operation is selected.")
  50.         End If
  51.     End Sub
  52.  
  53.  
  54.     Sub addition(ByVal num1 As Double, ByVal num2 As Double)
  55.         checknumber(num1, num2)
  56.         txtResult.Text = (num1 + num2)
  57.         'build list
  58.         lstResults.Items.Add(txtResult.Text)
  59.     End Sub
  60.  
  61.     Sub subtraction(ByVal num1 As Double, ByVal num2 As Double)
  62.         txtResult.Text = num1 - num2
  63.         'build list
  64.         lstResults.Items.Add(txtResult.Text)
  65.     End Sub
  66.  
  67.     Sub multiplication(ByVal num1 As Double, ByVal num2 As Double)
  68.         txtResult.Text = num1 * num2
  69.         'build list
  70.         lstResults.Items.Add(txtResult.Text)
  71.     End Sub
  72.  
  73.     Sub division(ByVal num1 As Double, ByVal num2 As Integer)
  74.         'validate input
  75.         If num2 = 0 Then
  76.             MsgBox("Number 2 cannot be 0.")
  77.             Exit Sub
  78.         End If
  79.         txtResult.Text = num1 / num2
  80.         'build list
  81.         lstResults.Items.Add(txtResult.Text)
  82.     End Sub
  83.     Sub modular(ByVal num1 As Double, ByVal num2 As Double)
  84.         'validate input
  85.         If num2 = 0 Then
  86.             MsgBox("Number 2 cannot be 0.")
  87.             Exit Sub
  88.         End If
  89.         txtResult.Text = num1 Mod num2
  90.         'build list
  91.         lstResults.Items.Add(txtResult.Text)
  92.     End Sub
  93.     Sub exponentiate(ByVal num1 As Double, ByVal num2 As Double)
  94.         'validate input
  95.         If num1 = 0 And num2 < 0 Then
  96.             MsgBox("input is invalid.")
  97.             Exit Sub
  98.         End If
  99.         txtResult.Text = num1 ^ num2
  100.         'build list
  101.         lstResults.Items.Add(txtResult.Text)
  102.     End Sub
  103.     Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  104.         lstResults.Items.Clear()
  105.         txtNum1.Text = ""
  106.         txtNum2.Text = ""
  107.         txtRound.Text = ""
  108.         txtResult.Text = ""
  109.         cmbOperation.Text = ""
  110.         radRound.Checked = False
  111.     End Sub
  112.  
  113.     Private Sub radRound_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radRound.CheckedChanged
  114.         'disable rounding option unless user wants to round
  115.         If radRound.Checked = True Then
  116.             'allow user to round
  117.             txtRound.Enabled = True
  118.         Else
  119.             'do not allow rounding
  120.             txtRound.Enabled = False
  121.         End If
  122.     End Sub
  123.  
  124.     Private Sub lstResults_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstResults.SelectedIndexChanged
  125.         If lstResults.SelectedIndex >= 0 Then
  126.             If MsgBox("Use this number in the calculation?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
  127.                 txtNum1.Text = lstResults.Text
  128.             End If
  129.         End If
  130.         If lstResults.SelectedIndex = -1 Then
  131.             MsgBox("no number is selected.")
  132.             Exit Sub
  133.         End If
  134.     End Sub
  135. End Class
Oct 10 '08 #1
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.
Oct 11 '08 #2
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.
Oct 11 '08 #3
jac130
18
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.
Oct 11 '08 #4
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.
Oct 11 '08 #5
jac130
18
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.
Oct 11 '08 #6
NeoPa
32,556 Expert Mod 16PB
Expand|Select|Wrap|Line Numbers
  1. Function checknumber(ByVal num1 As String, ByVal num2 As String) As Boolean
  2.   If Not IsNumeric(num1) Then
  3.     MsgBox("Input must be a number.")
  4.   ElseIf Not IsNumeric(num2) Then
  5.     MsgBox("Input must be a number.")
  6.     Return False
  7.   End If
  8. 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.
Oct 12 '08 #7
jg007
283 100+
can you merge the two threads here please.

** Admin edit - Spoonfeeding help removed **
Oct 12 '08 #8
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.
Oct 12 '08 #9
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.
Oct 12 '08 #10
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.
Oct 12 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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"...
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
3
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...
3
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...
24
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...
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...
5
Deathwing
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...
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,...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

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.