Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: VB calc code

  1. #1
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question VB calc code

    trying to do VBsimple calculator codes ..i got all the visible aspect done just need the code...can anyone give me calculator code?

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    you mean you dont know how to subtract, add, multiply and divide in code?
    I am the luckiest man in the world

  • #3
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    that & pretty much everything else to do w/ the code like allowing more then 1 digit in the input (text) box to make calculation i.e being able to add multiple digit #s not just single

  • #4
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    anyone?

  • #5
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    One nice thing about programming is that the number of digits in your entries would not matter...

    I.E.

    If I wanted to add two variables I would simply use

    C = A + B Where C becomes the result of A and B

    So for a calculator using a text box, you would have to use essentially 4 - 5 steps.

    1. Capture the first entry

    numVarA = InputBox1.Text

    2. Capture the function (addition, subtraction, multiplcation and division)

    funcVarOperation = whichever button is pushed (so if I pushed the addition button the funcVarOperation would store that I am going to do addition)
    InputBox1.Text = ""

    3. Capture the second entry

    numVarB = InputBox1.Text

    4. If equals is pushed (Run the function specified by funcVarOperation and print the results) else if another function is chosen (do the first function, clear one of your variables and continue with step 2)

    If equals is pushed
    varResults = funcVarOperation(numVarA,numVarB)
    InputBox1.Text = varResults
    else
    numVarA = funVarOperation(numVarA,numVarB)
    numVarB = ""
    End If
    funcVarOperation = whichever button is pushed (so if I pushed the addition button the funcVarOperation would store that I am going to do addition)
    InputBox1.Text = ""

    HTH,

    -saige-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #6
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks for info sage..only thing is whats code for the operators (+,-,/,*)?
    1 other thing..could someone provide couple of examples of if..else statements that could be utilized in a calc
    Last edited by jmoney3457; 11-02-2007 at 10:39 PM.

  • #7
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Well...

    C = A + B
    C = A - B
    C = A * B
    C = A / B or A \ B

    They are what you think they are...

    http://www.netcoole.com/VB6TOCS/vbhtml/vbopers.htm

    -saige-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #8
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    the #s (digits) & the operators on the form part would be clickable labels correct? also can someone write an example of an if-then-else statement for a calc? lastly, what is Converting variable types in VB mean?
    Last edited by jmoney3457; 11-03-2007 at 02:20 AM.

  • #9
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts
    No, lables are usually for text, you can have a button for each digit, or have a textbox for one value, and one for the other, then the user clicks the appropriate mathmatical operator, and you could display the result in a label.

    But with such a simple question, you should try google. If your just starting out, maybe try some VB tutorials? MSDN has like 30 free video tutorials that you could watch to keep you busy.

    as for converting variable types, a textbox by default is a string so in order to take 2 textboxes with numeric inputs and do somthing to them, youd need to parse the text box into an integer first.

    for example in C#: int.Parse(textbox1.Text);
    Last edited by Rappa; 11-03-2007 at 02:49 AM.

  • #10
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Rappa View Post
    No, lables are usually for text, you can have a button for each digit, or have a textbox for one value, and one for the other, then the user clicks the appropriate mathmatical operator, and you could display the result in a label.

    But with such a simple question, you should try google. If your just starting out, maybe try some VB tutorials? MSDN has like 30 free video tutorials that you could watch to keep you busy.

    as for converting variable types, a textbox by default is a string so in order to take 2 textboxes with numeric inputs and do somthing to them, youd need to parse the text box into an integer first.

    for example in C#: int.Parse(textbox1.Text);
    how do you parse the text box into integer 1st so the 2 txtboxes w/ num inputs can do something w/ them in VB since your example was for C#? also what could someone write out what a if then else statement in calculator would look like?

  • #11
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts
    i wont write the code, but ill give you some pseudocode:

    Code:
    If condition Then
    [statements]
    Else If condition Then
    [statements]
    -
    -
    Else
    [statements]
    End If
    or you can use a case statement, whos syntax is as such:
    Code:
    Select [ Case ] testexpression
        [ Case expressionlist
            [ statements ] ]
        [ Case Else
            [ elsestatements ] ]
    End Select
    heres sample code:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    cmbOperator.AddItem ("+")
    cmbOperator.AddItem ("-")
    cmbOperator.AddItem ("*")
    cmbOperator.AddItem ("/")
    End Sub
    
    Private Sub cmdCalculate_Click()
    If cmbOperator.Text = "" Then
    MsgBox ("You most chose a operator!!")
    Else
    Select Case cmbOperator.Text
    Case "+"
    
    Case "-"
    
    Case "*"
    
    Case "/"
    
    End Select
    lblResult.Caption = Val(txtNumber1.Text) + Val(txtNumber2.Text)
    lblResult.Caption = Val(txtNumber1.Text) - Val(txtNumber2.Text)
    lblResult.Caption = Val(txtNumber1.Text) * Val(txtNumber2.Text)
    lblResult.Caption = Val(txtNumber1.Text) / Val(txtNumber2.Text)
    End If
    End Sub
    Last edited by Rappa; 11-03-2007 at 04:20 AM.

  • #12
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thx rappa really appreciate it bro..just 1 last thing..how do you parse the 2 text boxes into integer? and when you say that what do you exactly mean?

  • #13
    New Coder
    Join Date
    Nov 2007
    Location
    Maine
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    anyone?

  • #14
    Regular Coder Rappa's Avatar
    Join Date
    Nov 2005
    Location
    LI
    Posts
    356
    Thanks
    21
    Thanked 9 Times in 9 Posts
    if you look at my code, i used the Val() function, which converts a string to an integer. Its not the best practice to use, but i assume your not dealing with percents so it souldnt matter. So Val() is a crude equal to C#'s int.Parse() function.

  • #15
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    In other words:

    Integers are whole numbers both positive and negative - i.e. {-34, -33, 5, 0, 5, 33, 34}

    If you wanted to use decimals you would use a float or long.

    -saige-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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