Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i do basic equations with what has been enterd in a text box?

Status
Not open for further replies.

splitsie

Programmer
Jun 29, 2001
7
0
0
AU
i have searched through the help files in msdn but i cant seem to find anything that can help me complete the simple task of adding subtracting multiplying or dividing the numbers that are entered into two text boxes

thanks in advance for the help
 
MyAnswer = CDbl(Text1.text) * CDbl(Text2.text)
returns a type 'double'
You will probably want to do some verification on what is entered in each text box(make sure it's a #) Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
simple:

Private Sub Command1_Click()
Text3.Text = Text1.Text + Text2.Text
End Sub

This quires that you have 3 textboxes and a command button.
The above function will add. To multiply, create another commandbutton like this:

Private Sub Command2_Click()
Text3.Text = Text1.Text * Text2.Text
End Sub

And so on :) Best Regards and many Thanks!
Michael G. Bronner X-)

"They who drink beer will think beer." Washington Irving
 
thanks for the help... timlarkin you are a saviour = )
 
You had better add some validation because if Text1.Text = "A" and Text2.Text = "1", you are going to meer Mr. Error 13, Type Mismatch. Use Kepress Event to "pre-edit"
Code:
Sub Text1_Keypress (KeyAscii as integer)
    Select Case KeyAscii
    Case Asc("0") to Asc("9"), 8 'Digits and Backspace Ok
    Case ELse
        KeyAscii = 0             ' Reject 
    End Select
End Sub
 
While I agree with JohnYingling on the necessity of vallidation checks before attempting to 'do the math', I know of no reason to implement/instantiate the resource expensive KeyPress event. It should be sufficient to check each of the source textboxes as "IsNumeric". Further, it should be fairly simple to create an option group for the operation [ "+" | "-" | "*" | "/" | "\" | ... "mod"] and use a simple select statement to perform single operations on the (Validated) values entered with the selected operator. At least conceptually, one should be able to build fairly complex statements using such an approach.

Since we cannot post forms here, I cannot show this "in action", however I have implemented a brief subset of the concept and it does work.


MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
thanks for all the help

it is much appreciated = )
 
i cant work out how to implement the checking of whether the text is numeric with the text boxes being in arrays

could someone please help? thankyou = )
 
Since my sample uses only two "sources" they are both required so this is all that I did:

Code:
    If (IsNumeric(txtSrcA(0).Text) And IsNumeric(txtSrcA(1).Text)) Then

Of course, to build a really complex statement you would need to handle the input process differently, and might have any number of sources. Then you would need to do it differently. For many sources, you could do the isnumeric in a loop, saving the ones which are numeric in a array (as values) and do the math from the array values.

You Coud also just place the entire expression in a single text box and parse through it one "token" at a time. If you did it this way you could build a "Stack" of the operators and operands (a-la the reverse polish notation used by many early calculators). Properly constructed, a stack like this can be used to evaluate virtually any statement where all of the operations are available. One of the early DEC computers had a fortran compiler which operated on this concept (PDP-11/45 I think).

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top