I am fairly new to c#. I have requirement of previous text and newly changed text of text box on text changed event of the same. I tried to get text on textchanged event but it is new text only. How can I get previous text also?
e.g. Say I have a text "abc" in my text box and I change it to "pqr" by pasting text directly and not by typing. now on text change event txtbox.text returns me "pqr" . But I need to compare previous and new text , so I need "abc" also. So how can I get it?
private void txtFinalTrans_TextChanged_1(object sender, EventArgs e)
{
gstrOldText = txtFinalTrans.Text;
}
The event handler is called whenever the contents of the TextBox control are changed, either by a user or programmatically. This event fires when the TextBox control is created and initially populated with text.
The TextChanged event is raised when the content of the text box changes between posts to the server. The event is only raised if the text is changed by the user; the event is not raised if the text is changed programmatically.
Try creating a global variable and put your textbox text during GotFocus
event and use it as Old Text during TextChanged
event as like:
string OldText = string.Empty;
private void textBox1_GotFocus(object sender, EventArgs e)
{
OldText = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string newText = textBox1.Text;
//Compare OldText and newText here
}
Hope this helps...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With