C# - TextBox_TextChanged事件 - 还原到以前的值

本文关键字:事件 TextBox TextChanged 还原 | 更新日期: 2023-09-27 18:30:43

我正在发布一个关于TextBox_TextChanged事件的确认对话框。如果用户点击"否",我想以某种方式将文本框恢复为其旧值(即更改之前)但是在触发事件时,文本框.文本已经是更改的值...有没有办法保存或获取旧值?

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

这是我的代码:

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

C# - TextBox_TextChanged事件 - 还原到以前的值

选项 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:如果您只需要最后一个文本(意味着只向后退一步),则可以使用 text changed 事件在更改之前使用当前文本更新字符串变量,然后您可以在任何您想要的地方使用它。

我要做的(不确定它是否是最好的选择)是创建一个变量并在 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;
               }
        }