Click here to Skip to main content
15,894,132 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have 3 textboxes that a user will supply values for. When they have valid values for all three textboxes, how do I automatically calculate a value and display it in a 4th textbox?
Posted
Updated 22-Jun-10 14:55pm
v2
Comments
Sandeep Mewara 22-Jun-10 17:09pm    
Question not framed properly. Further is this web or windows Q?

Well, unless you force the user to enter the values in order (first input in TextBox1, second input in TextBox2 and third input in TextBox3), you need to hook all three TextBoxChanged events.

Thankfully, you can write a single method to do this and just attach it to each TextBoxChanged event. Then, just check to make sure there are values in each of the three boxes and do your calculations.
 
Share this answer
 
You can write your calculation code in "onchange" event of 3rd textbox
 
Share this answer
 
Comments
William Winner 22-Jun-10 18:03pm    
Reason for my vote of 2
true, but not complete. What happens if they enter the value in TextBox3 first, and then go on to the other TextBoxes? Or, if they change a value in TextBox1 after entering TextBox 3?
Homework again, write the calculation code(write a single function) in 4th 1st, 2nd & 3rd textboxes TextChanged event. Here after don't ask such questions try yourself first if you find any issues post your code which you have tried & ask help here.
 
Share this answer
 
v5
Comments
William Winner 22-Jun-10 18:04pm    
Reason for my vote of 1
the 4th textbox is the one that the calculation is going into. Why would you want to hook that one?
VB
Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
        Dim NewTextBox As Boolean = True
        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                If CType(ctrl, TextBox).Text = "" Then
                    NewTextBox = False
                End If
            End If
        Next
        If NewTextBox Then
            Dim newtext = New TextBox
            newtext.Location = New Point(300, 300)
            newtext.Size = New Size(150, 150)
            newtext.Text = "ehsan"
            Me.Controls.Add(newtext)
        End If
    End Sub
 
Share this answer
 
Comments
Sandeep Mewara 22-Jun-10 17:07pm    
Reason for my vote of 1
What answer is this? How come it's related to what is asked?
William Winner 22-Jun-10 18:06pm    
Reason for my vote of 1
why are you posting an answer from part of a previous question? Though I'm glad to see you added what I suggested.

By the way, the name "NewTextBox" implies that it's a textbox. Standard naming conventions would suggest changing to something like "boolNewTextBox".
Smithers-Jones 22-Jun-10 18:39pm    
Reason for my vote of 1
Do not throw bits of code. Not a sensible answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900