Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get old text and changed text of textbox on TextChanged event of textbox?

Tags:

c#

.net

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;              
}
like image 352
Anonymous Avatar asked Dec 05 '14 07:12

Anonymous


People also ask

Which event is generated when TextBox text is changed?

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.

Which event is fired when there is a change in the contents of the TextBox?

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.


1 Answers

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...

like image 63
Vanest Avatar answered Sep 28 '22 04:09

Vanest