3

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]
    }
eranfu
  • 55
  • 1
  • 9

2 Answers2

0

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;
               }
        }
Koen
  • 634
  • 3
  • 14
  • 5
    @Coulton: Don't judge a post on the lack of code. If a post is good, it needs no code. – Patrick Hofman Nov 18 '14 at 14:14
  • So i added some code, doesn't really make a difference but if it pleases you. – Koen Nov 18 '14 at 14:15
  • @Coulton: I didn't say it was good. The note was just general. Also, I appreciate more information on the solution that just a code drop. – Patrick Hofman Nov 18 '14 at 14:16
  • I said add some code, you did. What's the problem? :| – Luke Nov 18 '14 at 14:19
  • 2
    @Koen Be carefull - setting the `Text` property in the `TextChanged` event might set off the event, and create a loop. – Jens Kloster Nov 18 '14 at 14:23
  • 1
    @Coulton, this is not a tutorial site; giving a valid and explanatory suggestion itself is a valid answer which needs no code. Have OP figure out something by himself else it will become spoon feeding. – Rahul Nov 18 '14 at 14:24
  • 1
    Can you guys please get back onto the question at hand? – Luke Nov 18 '14 at 14:26
  • 3
    Why on earth would it have to be static? –  Nov 18 '14 at 14:31
0

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.

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • 1
    Luckily I need to take only one step backwards, so option 2 works great for me. Just need to add a condition for preventing the event from triggering itself, i.e. `if (txtFCServerURL.txt != existingURL)` ' – eranfu Nov 23 '14 at 16:53