C# - TextBox_TextChanged事件 - 恢复到以前的值

时间:2014-11-18 14:11:10

标签: c# textbox textchanged

我在TextBox_TextChanged事件上发布确认对话框。 如果用户点击“否”,我想以某种方式将文本框还原为其旧值(即在更改之前) 但是在触发事件时,TextBox.Text已经是更改后的值... 有没有办法保存或获得旧值?

欣赏任何想法或方法。谢谢!

这是我的代码:

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 个答案:

答案 0 :(得分:0)

选项1:文本框获得Undo方法。 这是一个代码示例的链接:

http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.undo%28v=vs.110%29.aspx

为了方便起见,这是链接中的示例:

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();
    }
 }

选项2:如果您只需要最后一个文本(意味着只需向后一步),您可以使用文本更改事件来更新字符串变量,并使用当前文本更新字符串变量&#39;改变了,然后你可以随时随地使用它。

答案 1 :(得分:-1)

我会做什么(不确定它是否是最佳选择)是创建变量并在TextChanged结束时设置其值。这样,下次进入TextChanged时,您仍然可以获得之前更改的值。

 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;
               }
        }