Hey
Does anyone seen a way to do division in a text box?
Reston
Printable View
Hey
Does anyone seen a way to do division in a text box?
Reston
vb Code:
Private Sub Command1_Click() If Not IsNumeric(Text1.Text) Then MsgBox ("Only Numbers please =)") ElseIf Not IsNumeric(Text2.Text) Then MsgBox ("Only numbers please =)") Else Text3.Text = Text1.Text / Text2.Text End If End Sub
I'm looking for division in the same textbox
Something like 15/25.4
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:
Private Sub Command1_Click() Dim divi() As String Dim nfirst As String Dim nsecond As String Dim i As Integer 'Split the numbers divi() = Split(Text1.Text, "/") 'Loop through, and get your numbers For i = LBound(divi) To UBound(divi) 'Set first number nfirst = divi(0) 'Set second number nsecond = divi(1) 'Loop till it gets both numbers Next i 'Divide the numbers Text2.Text = divi(0) / divi(1) End Sub
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
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)
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
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?
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.
The textbox can be empty. As long as it exists, it can get focus.
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.Quote:
Originally Posted by Neato
So, back to the original questionYes. And I would use this approach as posted by user nameQuote:
Originally Posted by tiguy
This sends the output to the immediate window. Where do you really want the output to go?Code:'project >> references >> microsoft script control 1.0
Dim msc As New MSScriptControl.ScriptControl
msc.Language = "vbscript"
Debug.Print msc.Eval(Text1.Text)
And what would you be doing with this number?Quote:
Originally Posted by tiguy
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
Ok....this is a good approach.Quote:
Originally Posted by tiguy
Did you get it working or did you run into a problem?
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
Well, except for Error Trapping, GoTo should be avoided.
I suggest you use user_name's approach since that handles all expressions including those with parentheses.