C# - TextBox_TextChanged event - revert to previous value

5.6k views Asked by At

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]
    }
2

There are 2 answers

1
CodeMonkey On BEST ANSWER

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:

private void Menu_Copy(System.Object sender, System.EventArgs e)


{
    // Ensure that text is selected in the text box.    
    if(textBox1.SelectionLength > 0)
        // Copy the selected text to the Clipboard.
        textBox1.Copy();
 }

 private void Menu_Cut(System.Object sender, System.EventArgs e)
 {   
     // Ensure that text is currently selected in the text box.    
     if(textBox1.SelectedText != "")
        // Cut the selected text in the control and paste it into the Clipboard.
        textBox1.Cut();
 }

 private void Menu_Paste(System.Object sender, System.EventArgs e)
 {
    // Determine if there is any text in the Clipboard to paste into the text box. 
    if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
    {
        // Determine if any text is selected in the text box. 
        if(textBox1.SelectionLength > 0)
        {
          // Ask user if they want to paste over currently selected text. 
          if(MessageBox.Show("Do you want to paste over current selection?", "Cut Example", MessageBoxButtons.YesNo) == DialogResult.No)
             // Move selection to the point after the current selection and paste.
             textBox1.SelectionStart = textBox1.SelectionStart + textBox1.SelectionLength;
        }
        // Paste current text in Clipboard into text box.
        textBox1.Paste();
    }
 }


 private void Menu_Undo(System.Object sender, System.EventArgs e)
 {
    // Determine if last operation can be undone in text box.    
    if(textBox1.CanUndo == true)
    {
       // Undo the last operation.
       textBox1.Undo();
       // Clear the undo buffer to prevent last action from being redone.
       textBox1.ClearUndo();
    }
 }

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.

8
Koen On

What i would do(not sure if it is the best option) is to make a variable and set its value at the end of your TextChanged. that way, the next time it will enter the TextChanged, you will still have the value of your previous change.

 string txt = "";
    private void txtFCServerURL_TextChanged(object sender, EventArgs e)
        {
              if(txtFCServerURL.Text != txt)
               {
                 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 = txt;
               }
        }