I'm posting a confirmation dialog on TextBox_TextChanged event. If the user hits 'No', I'd like to somehow revert the textbox to its old value (i.e. before it was changed) But at the point the event is triggered, the TextBox.Text is already the changed value... Is there a way to save or get to the old value?
Appreciate any ideas or approaches. Thanks!
Here's my code:
private void txtFCServerURL_TextChanged(object sender, EventArgs e)
{
DialogResult clearGrid = MessageBox.Show("Changing the text will clear the grid. Are you sure?", "Confirmation", MessageBoxButtons.YesNo);
if (clearGrid == DialogResult.Yes)
{
for (int i = 0; i < dgvGrid.Rows.Count; i++)
{
dgvGrid.Rows.RemoveAt(0);
}
}
else txtFCServerURL.Text = [TEXT BEFORE CHANGE]
}
Option 1: A textbox got an
Undo
method. Here is a link with a code example:http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.undo%28v=vs.110%29.aspx
Just to make it easier, this is the example from the link:
Option 2: If you only need the last text (meaning only a single step backward), you can use the text changed event to update a string variable with the current text before it's changed and then you can just use it wherever you want.