-
Sep 9th, 2012, 10:49 AM
#1
Thread Starter
Member
[RESOLVED] Calculator idea help
I'm trying to make a really simple calculator, which looks like this:
The user should enter values in the three big textboxes, and between those enter either +, -, x or /. Pressing the button should calculate the answer. Now here's the question, how do i make it take what's in the smaller textboxes and use it in the code? Hard to explain, but let me show you how i was thinking.
Instead of this: richtextbox1.text = val(textbox1.text) * val(textbox1.text) I want to replace the * with what's in the small textboxes, kinda like this: richtextbox1.text = val(textbox1.text) (richtextbox1.text) val(textbox2.text)
thanks in advance
-
Sep 9th, 2012, 11:14 AM
#2
Lively Member
Re: Calculator idea help
Instead of this: richtextbox1.text = val(textbox1.text) * val(textbox1.text) I want to replace the * with what's in the small textboxes, kinda like this: richtextbox1.text = val(textbox1.text) (richtextbox1.text) val(textbox2.text)
i don't really understand about your request... can you explain it more clearly?
-
Sep 9th, 2012, 11:18 AM
#3
Thread Starter
Member
Re: Calculator idea help
Originally Posted by chipp
i don't really understand about your request... can you explain it more clearly?
Replace the * with what's in the small boxes
-
Sep 9th, 2012, 11:21 AM
#4
New Member
Re: Calculator idea help
Here's how I would do it (with your design)
Code:
If rtxtBox1.text = "+" Then
txtBox3.Text = Val(textbox1.text) + Val(txtBox2)
ElseIf rtxtBox1.Text = "-" Then
txtBox3.Text = Val(textbox1.text) - Val(txtBox2)
ElseIf rtxtBox1.Text = "*" or "x" Then
txtBox3.Text = Val(textbox1.text) * Val(txtBox2)
ElseIf rtxtBox1.Text = "/" Then
txtBox3.Text = Val(textbox1.text) / Val(txtBox2)
End if
-
Sep 9th, 2012, 11:24 AM
#5
Junior Member
Re: Calculator idea help
I would use if statements...
Not sure what the names of all your objects are... but ---- lets say textbox1 is what the user puts their arithmetic expression in and richtextbox1 is number 1, richtextbox 2 is number 2, and richtextbox 3 is the answer:
If textbox1.text = "*" then richtextbox3.text = val(richtextbox1.text) * val(richtextbox2.text)
If textbox1.text = "/" then richtextbox3.text = val(richtextbox1.text) / val(richtextbox2.text)
and so on, and so on....
-
Sep 9th, 2012, 11:30 AM
#6
Re: Calculator idea help
What powerstack said but with Select Case (only cos it's less typing!)
vb.net Code:
Select Case textbox1.text
Case "+"
'do the addition
Case "-"
'do the subtraction
etc.
End Select
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 9th, 2012, 12:15 PM
#7
Re: Calculator idea help
Originally Posted by dunfiddlin
(only cos it's less typing!)
...plus it's better looking
-
Sep 9th, 2012, 04:23 PM
#8
Re: Calculator idea help
try this:
vb.net Code:
dim dt as new datatable richtextbox1.text = dt.compute(textbox1.text & richtextbox1.text & textbox2.text, nothing).tostring
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 9th, 2012, 04:30 PM
#9
Re: Calculator idea help
Originally Posted by .paul.
try this:
vb.net Code:
dim dt as new datatable richtextbox1.text = dt.compute(textbox1.text & richtextbox1.text & textbox2.text, nothing).tostring
Oooh! Cunning!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 9th, 2012, 04:33 PM
#10
Re: Calculator idea help
good looking too
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 10th, 2012, 01:15 PM
#11
Thread Starter
Member
Re: Calculator idea help
Originally Posted by .paul.
try this:
vb.net Code:
dim dt as new datatable richtextbox1.text = dt.compute(textbox1.text & richtextbox1.text & textbox2.text, nothing).tostring
Thanks paul!
view this thread for finished program: http://www.vbforums.com/showthread.p...31#post4233531
-
Sep 10th, 2012, 01:41 PM
#12
Hyperactive Member
Re: Calculator idea help
Originally Posted by .paul.
try this:
vb.net Code:
dim dt as new datatable
richtextbox1.text = dt.compute(textbox1.text & richtextbox1.text & textbox2.text, nothing).tostring
Now that's thinking outside the box! Never in a million years could I've come up with a solution like that.
Last edited by DavesChillaxin; Sep 10th, 2012 at 01:42 PM.
Reason: added quote
-
Sep 10th, 2012, 01:43 PM
#13
Thread Starter
Member
Re: Calculator idea help
Originally Posted by DavesChillaxin
Now that's thinking outside the box! Never in a million years could I've come up with a solution like that.
I know right!
-
Sep 10th, 2012, 09:01 PM
#14
Junior Member
Re: Calculator idea help
Yep --- I think Paul wins the contest for sure....
But Erik --- Let me give you a suggestion for something else to try.... Try to make a calculator that's like the Microsoft Calculator that comes with Windows.... That was one of my first projects because math is one of the first things you learn in VB and it just sounded like fun to try and do. I did a pretty spiffy job I think. You just have to think about what happens when the user hits the number buttons and the numbers appear in the textbox ---- simple math won't do it. You have to use strings to get the job done and then convert those strings when it's time to do the math.
With mine I did all the basic arithmatic functions plus a memory +, memory -, and memory recall button. And just to impress my wife, I made a button called "secret" that prints "I love [wife's name]!!!!" . I never did show her the calculator program (I work overseas so I do a lot of my coding on a different computer than my home one) but I'll get around to it one day...
Ahhh, nerd love --- ain't it grand? =)
-
Sep 11th, 2012, 01:55 PM
#15
Thread Starter
Member
Re: Calculator idea help
Originally Posted by .paul.
try this:
vb.net Code:
dim dt as new datatable richtextbox1.text = dt.compute(textbox1.text & richtextbox1.text & textbox2.text, nothing).tostring
Thanks again, but could you explain what "nothing" and "tostring" does/mean? I'd like to understand everything that i put in my programs
-
Sep 11th, 2012, 02:16 PM
#16
Re: Calculator idea help
dt.compute takes 2 parameters. 1st is expression to compute, 2nd is filter + in my example it's unused. hence nothing
dt.compute returns an object so we need to convert it to a string hence toString
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
|