|
-
Nov 19th, 2007, 10:15 PM
#1
Thread Starter
Lively Member
division in text box
Hey
Does anyone seen a way to do division in a text box?
Reston
-
Nov 19th, 2007, 11:05 PM
#2
Lively Member
Re: division in text box
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
-
Nov 19th, 2007, 11:15 PM
#3
Thread Starter
Lively Member
Re: division in text box
I'm looking for division in the same textbox
Something like 15/25.4
-
Nov 19th, 2007, 11:50 PM
#4
Lively Member
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:
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
-
Nov 20th, 2007, 12:07 AM
#5
Lively Member
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
-
Nov 20th, 2007, 02:09 AM
#6
Hyperactive Member
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)
-
Nov 20th, 2007, 12:03 PM
#7
Thread Starter
Lively Member
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
-
Nov 20th, 2007, 12:35 PM
#8
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?
-
Nov 20th, 2007, 01:09 PM
#9
Lively Member
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.
-
Nov 20th, 2007, 01:10 PM
#10
Re: division in text box
The textbox can be empty. As long as it exists, it can get focus.
-
Nov 20th, 2007, 01:11 PM
#11
Lively Member
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.
-
Nov 20th, 2007, 01:21 PM
#12
Re: division in text box
 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
 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?
 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?
-
Nov 27th, 2007, 02:57 PM
#13
Thread Starter
Lively Member
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
-
Nov 28th, 2007, 07:25 AM
#14
Re: division in text box
 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?
-
Dec 3rd, 2007, 08:45 PM
#15
Thread Starter
Lively Member
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
-
Dec 4th, 2007, 01:39 PM
#16
Re: division in text box
Well, except for Error Trapping, GoTo should be avoided.
-
Dec 4th, 2007, 08:00 PM
#17
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|