【问题标题】:C# - TextBox_TextChanged event - revert to previous valueC# - TextBox_TextChanged 事件 - 恢复到以前的值
【发布时间】:2014-11-18 14:11:09
【问题描述】:

我正在发布关于 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]
    }

【问题讨论】:

  • 使用 Enter 事件将 Text 属性的值存储在变量中。
  • @eranfu 我已经给你答案了。你应该接受它
  • 是的,我花了一些时间来测试它。坦克!

标签: c# textbox textchanged


【解决方案1】:

选项 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 事件在字符串变量更改之前将其更新为当前文本,然后您可以随心所欲地使用它。

【讨论】:

  • 幸运的是,我只需要后退一步,所以选项 2 对我来说非常有用。只需要添加一个条件防止事件自行触发,即if (txtFCServerURL.txt != existingURL)'
【解决方案2】:

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

【讨论】:

  • @Coulton:不要因为缺少代码而评判帖子。如果一个帖子很好,它不需要代码。
  • 所以我添加了一些代码,并没有什么不同,但如果它让你满意的话。
  • @Koen 小心 - 在 TextChanged 事件中设置 Text 属性可能会引发事件,并创建一个循环。
  • @Coulton,这不是一个教程网站;给出一个有效和解释性的建议本身就是一个不需要代码的有效答案。让 OP 自己想办法,不然就变成勺子喂了。
  • 为什么它必须是静态的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 2017-11-29
  • 1970-01-01
相关资源
最近更新 更多