Results 1 to 17 of 17

Thread: division in text box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    division in text box

    Hey

    Does anyone seen a way to do division in a text box?

    Reston

  2. #2
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: division in text box

    vb Code:
    1. Private Sub Command1_Click()
    2. If Not IsNumeric(Text1.Text) Then
    3.     MsgBox ("Only Numbers please =)")
    4. ElseIf Not IsNumeric(Text2.Text) Then
    5.     MsgBox ("Only numbers please =)")
    6. Else
    7.     Text3.Text = Text1.Text / Text2.Text
    8. End If
    9. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: division in text box

    I'm looking for division in the same textbox

    Something like 15/25.4

  4. #4
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: division in text box

    This oughta do ya. You can switch it around for different functions ie. Add command controls with different settings for mulitplication, addition, subtraction, ect. Hope this helps.

    vb Code:
    1. Private Sub Command1_Click()
    2. Dim divi() As String
    3. Dim nfirst As String
    4. Dim nsecond As String
    5. Dim i As Integer
    6. 'Split the numbers
    7. divi() = Split(Text1.Text, "/")
    8. 'Loop through, and get your numbers
    9. For i = LBound(divi) To UBound(divi)
    10. 'Set first number
    11. nfirst = divi(0)
    12. 'Set second number
    13. nsecond = divi(1)
    14. 'Loop till it gets both numbers
    15. Next i
    16. 'Divide the numbers
    17. Text2.Text = divi(0) / divi(1)
    18. End Sub

  5. #5
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Re: division in text box

    and yet another way...

    Code:
    Private Sub CommandButton1_Click()
    Dim dividend As String
    Dim divisor As String
    Dim info As String
    Dim Split As Integer
    
    ' Start with this.
    info = TextBox1.Text
    ' Find the Division.
    Split = InStr(1, info, "/", vbTextCompare)
    
    dividend = Left(info, Split - 1)
    
    divisor = Right(info, Split + 1)
    Label1.Caption = dividend / divisor
    
    End Sub
    It's all about Attitude!

  6. #6
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: division in text box

    And another.
    Code:
    'project >> references >> microsoft script control 1.0
    
    Dim msc As New MSScriptControl.ScriptControl
    
    msc.Language = "vbscript"
    
    Debug.Print msc.Eval(Text1.Text)

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: division in text box

    This works nice. Thanks everyone.

    Can this work like this? Lets say I input 15/25.4 and I have already written code that moves the cursor to the next textbox it automatically calculates the division if there is a division in the textbox? On the other had I may have a straight variable in the textbox like 1062.

    Reston

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: division in text box

    I'm not sure I understand.

    Do you want the answer to go into a second text box, or do you want calculations done on a second textbox after the calculations are done on the first textbox?

  9. #9
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: division in text box

    I believe what he's asking is having multiple textboxes that would he would SetFocus on after the prior has been calculated. You'd have to write the code for each textbox, but yes. You'd just need to add an error handling procedure in case the textbox you setfocus on is empty.
    Please use the search function prior to posting a question and see if someone's already answered it.
    -If I helped you, please rate me, as I'd do the same for you =)
    -Remember to select the Resolved option for your post when you've gotten the answers you need.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: division in text box

    The textbox can be empty. As long as it exists, it can get focus.

  11. #11
    Lively Member
    Join Date
    Nov 2007
    Location
    Rochester, NY
    Posts
    111

    Re: division in text box

    What I meant, is that if it's empty, it would move on to the next textbox in the line, instead of running the calculator function.
    Please use the search function prior to posting a question and see if someone's already answered it.
    -If I helped you, please rate me, as I'd do the same for you =)
    -Remember to select the Resolved option for your post when you've gotten the answers you need.

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: division in text box

    Quote Originally Posted by Neato
    What I meant, is that if it's empty, it would move on to the next textbox in the line, instead of running the calculator function.
    Ah, OK....gotcha.

    So, back to the original question
    Quote Originally Posted by tiguy
    Can this work like this? Lets say I input 15/25.4 and I have already written code that moves the cursor to the next textbox it automatically calculates the division if there is a division in the textbox?
    Yes. And I would use this approach as posted by user name
    Code:
    'project >> references >> microsoft script control 1.0
    
    Dim msc As New MSScriptControl.ScriptControl
    
    msc.Language = "vbscript"
    
    Debug.Print msc.Eval(Text1.Text)
    This sends the output to the immediate window. Where do you really want the output to go?
    Quote Originally Posted by tiguy
    On the other had I may have a straight variable in the textbox like 1062.
    And what would you be doing with this number?

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: division in text box

    Hey

    Thanks for the help. M. Nolan's post seemed to work fine. I'm now trying to add an if statement that checks to see if a division is present. That way i don't get an error.

    Reston

  14. #14
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: division in text box

    Quote Originally Posted by tiguy
    I'm now trying to add an if statement that checks to see if a division is present. That way i don't get an error.
    Ok....this is a good approach.

    Did you get it working or did you run into a problem?

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: division in text box

    It works fine. I added an if statement.

    Vbcode
    info = TextBox1.Text
    ' Find the Division.
    Split = InStr(1, info, "/", vbTextCompare)
    If Split = 1 then goto A:

    And A: is down past all the other code for this. Not sure if this is the best way but it works.

    Reston

  16. #16
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: division in text box

    Well, except for Error Trapping, GoTo should be avoided.

  17. #17
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: division in text box

    I suggest you use user_name's approach since that handles all expressions including those with parentheses.

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