Results 1 to 16 of 16

Thread: [RESOLVED] Calculator idea help

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Resolved [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

  2. #2
    Lively Member chipp's Avatar
    Join Date
    May 2012
    Posts
    78

    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?

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: Calculator idea help

    Quote Originally Posted by chipp View Post
    i don't really understand about your request... can you explain it more clearly?
    Replace the * with what's in the small boxes

  4. #4
    New Member
    Join Date
    Sep 2012
    Posts
    6

    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

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    20

    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....

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Calculator idea help

    What powerstack said but with Select Case (only cos it's less typing!)

    vb.net Code:
    1. Select Case textbox1.text
    2. Case "+"
    3. 'do the addition
    4. Case "-"
    5. 'do the subtraction
    6. etc.
    7. 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!

  7. #7
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Calculator idea help

    Quote Originally Posted by dunfiddlin View Post
    (only cos it's less typing!)
    ...plus it's better looking
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Calculator idea help

    try this:

    vb.net Code:
    1. dim dt as new datatable
    2. richtextbox1.text = dt.compute(textbox1.text & richtextbox1.text & textbox2.text, nothing).tostring

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Calculator idea help

    Quote Originally Posted by .paul. View Post
    try this:

    vb.net Code:
    1. dim dt as new datatable
    2. 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!

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Calculator idea help

    good looking too

  11. #11

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: Calculator idea help

    Quote Originally Posted by .paul. View Post
    try this:

    vb.net Code:
    1. dim dt as new datatable
    2. 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

  12. #12
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Calculator idea help

    Quote Originally Posted by .paul. View Post
    try this:

    vb.net Code:
    1. dim dt as new datatable
    2. 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
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  13. #13

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: Calculator idea help

    Quote Originally Posted by DavesChillaxin View Post
    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!

  14. #14
    Junior Member
    Join Date
    Apr 2012
    Posts
    20

    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? =)

  15. #15

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: Calculator idea help

    Quote Originally Posted by .paul. View Post
    try this:

    vb.net Code:
    1. dim dt as new datatable
    2. 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

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width