-
Feb 1st, 2019, 04:50 PM
#1
Thread Starter
Member
[RESOLVED] Sum of two textbox
Hello VBForums
Hello every one
Please if you can help to resolve this problem
I have for example TextBox1 and TextBox2
I want ..
When I type a number in TextBox1 for example ( 12.76 ) appears automatically in TextBox2 ( 12.76 ) .. when I type a second digit a second time in TextBox1 for example ( 5.11 ) appears in TextBox2 the sum ( 17.87 ) .. when I type a Third digit a third time in TextBox1 for example ( 10.59 ) appears in TextBox2 the sum ( 28.46 ) .. and so on
Thank you in advance for help
Cordially
ABIDINE
-
Feb 1st, 2019, 05:27 PM
#2
Re: Sum of two textbox
Your phrasing is a bit confusing, Where you say digit you seem to actually mean a number that in the case of your examples is more than one digit.
You will need to use a trigger of some sort, either trap the enter key when pressed or use a button press so it will add after they enter how ever many digits are needed.
Then you basically add the value of textbox1. to the value of textbox2
Of course you need to insure that the value entered are actually valid numbers before you try to add them or else you will get unexpected results.
-
Feb 1st, 2019, 05:28 PM
#3
Re: Sum of two textbox
Declare a Double variable at the Form level, convert the String in the TextBox to a Double, and then add it to the variable. Something like this (assuming a Button submits the value):
Code:
Public Class Form1
'Declare a Double variable at the Form level
Private total As Double = 0
'Submit the value input
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'Declare a value to store the Double value of the TextBox input
Dim value As Double
'Attempt to convert the String in the TextBox to a Double
If Double.TryParse(TextBox1.Text, value) Then
'Add the value to the total
total += value
'Display the new total
TextBox2.Text = total.ToString()
Else
'Invalid input
MessageBox.Show("Invalid number entered in TextBox1", "Invalid Input")
End If
End Sub
End Class
-
Feb 1st, 2019, 05:36 PM
#4
Thread Starter
Member
Re: Sum of two textbox
Thank you DataMiser for logic explanation
dday9 ..thank you very much for help
But i have 2 problem
1-If Textbox2 is not empty here I will get an error because when i click Button1 .. I got only the value of TextBox1 in TextBox2
2- I would like to have help with the event TextBox _Change
Thank you a lot
Cordially
ABIDINE
-
Feb 1st, 2019, 05:44 PM
#5
Re: Sum of two textbox
Finding the right trigger is tricky, in this case. The Change event might work, but you'd have to see what happened with Copy and Paste. You may not get what you want. Some events don't get triggered on Paste, and I don't recall whether that one does or not.
Another issue that you may have to contend with is the backspace. The code DDay showed will add every time the button is clicked. Using a button is certainly safer, because then you know EXACTLY when to add. The problem is that once you have added...it's added. Hit the button a second time and you've added in the value a second time. There's no going back. That's a problem with no simple solution, too, because you can always come up with a sequence of actions that will cause you trouble, hence the CE button on calculators, which is only a partial solution in itself.
I would suggest considering using NumericUpDown controls such that you don't have to do the parsing, but that's actually the least of your troubles, in this case. That backspace problem is MUCH harder to solve.
My usual boring signature: Nothing
-
Feb 1st, 2019, 05:56 PM
#6
Re: Sum of two textbox
Originally Posted by ABIDINE
1-If Textbox2 is not empty here I will get an error because when i click Button1 .. I got only the value of TextBox1 in TextBox2
This isn't an error because the value would be placed in TextBox2. The behavior you described in your original post is how the code I provided you would behave:
- TextBox1 and TextBox2 start off with no value.
- A valid Double (12.76) is typed into TextBox1, the Button is clicked, the total value (12.76) is now transferred to TextBox2
- A valid Double (5.11) is typed into TextBox1, the Button is clicked, the total value (17.87) is now in TextBox2
- etc...
Originally Posted by ABIDINE
2- I would like to have help with the event TextBox _Change
You might run into some errors with this. Not only with what Shaggy Hiker suggested but also you're wanting to validate Doubles. For example: the value 12 is technically a valid Double even though it is an Integer how will you know if the user is entering in a Double value or Integer value? This is why I suggested using a Button to trigger the submission.
-
Feb 2nd, 2019, 11:23 AM
#7
Thread Starter
Member
Re: Sum of two textbox
Thank you very very much dday9
Thank you for help and explanation and best code
Perfectly resolved
Cordially
ABIDINE
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
|