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 to change the value in a textbox to the previous value?

Status
Not open for further replies.

Immortal49

Technical User
Apr 3, 2012
5
0
0
GB
Hi,

I have two textboxes, value in textbox1 gets added to the value in textbox2 and i have this coding in place, the only problem i have is that if i made an error when adding a value in textbox1 and i remove the value to add the correct value the textbox2 value does not also change to the previous value so the calculations become wrong. My coding so far.

Code:

So ideally what i was looking for was help so when i remove the value in textbox1 or just change it to 0 then the original value is displayed in textbox2? hope that makes sense.
 
Coding is not appearing so i will paste it below

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim Value1, Value2 As Decimal

Decimal.TryParse(TextBox1.Text, Value1)
Decimal.TryParse(TextBox2.Text, Value2)

TextBox2.Text = (Value1 + Value2).ToString
End Sub
 
Every time TextBox1'a Text Changes, the TextBox2's Text will change as well because that's what your last line of code does.

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 

You need to store the original value of TextBox2 somewhere, so if TextBox1 is blanked or set to 0 you can restore the original TextBox2 value. I suggest using textBox2's Tag property:

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

TextBox2.Tag = TextBox2.Text

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim Value1, Value2 As Decimal

If TextBox1.Text.Length = 0 Or TextBox1.Text = "0" Then
TextBox2.Text = TextBox2.Tag
End If

Decimal.TryParse(TextBox1.Text, Value1)
Decimal.TryParse(TextBox2.Text, Value2)

TextBox2.Text = (Value1 + Value2).ToString
End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 

Oops, just realized that the above code won't work, because changing textbox1 will change textbox2, which will change the Tag value as well. You will need something like a global flag that you set when textbox1 is being changes, so you don't change textbox2's Tag value:

Dim bText1Changing As Boolean = False 'declare this at the top of the form, in Declarations

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

If Not bText1Changing then
TextBox2.Tag = TextBox2.Text
End If

End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim Value1, Value2 As Decimal

bText1Changing = True

If TextBox1.Text.Length = 0 Or TextBox1.Text = "0" Then
TextBox2.Text = TextBox2.Tag
End If

Decimal.TryParse(TextBox1.Text, Value1)
Decimal.TryParse(TextBox2.Text, Value2)

TextBox2.Text = (Value1 + Value2).ToString

bText1Changing =False

End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top