我正在为TextBox开发一个非常简单的撤销功能,我有一个奇怪的问题。当我尝试从包含所有更改的Stack
t中取出字符串并将它们放在文本框中时,我看不到任何更改。
我做了一个小调试标签,以检查这是否真的有效。我发现它在标签中工作,但在文本框中它使用自己的撤销功能。
有没有办法取消或覆盖文本框撤消并使用我自己的功能?
以下是我所做的更改的示例代码:
private void Form1_KeyDown(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.Z && (ModifierKeys & Keys.Control) == Keys.Control)
{
nameTextBox.Text = undoName.GetLastChange(); //--> not working
undoDebuglabel.Text = undoName.GetLastChange(); --> working
}
}
GetLastChange()从类中的Stack
获取信息。
这就像文本框不让我看到变化。可能是因为我使用相同的快捷方式,CTRL + Z
来做它?
使用ClearUndo方法清除Textbox自己的堆栈。试试这个:
nameTextBox.ClearUndo();
nameTextBox.Text = undoName.GetLastChange();
您可以通过继承System.Windows.Forms.TextBox来创建自己的TextBox来处理历史记录。看看我的样本:
public class HistoryTextBox: System.Windows.Forms.TextBox
{
bool ignoreChange = false;
List<string> storage = null;
protected override void OnCreateControl()
{
base.OnCreateControl();
//init storage...
storage = new List<string>();
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
//save change to storage...
if (!ignoreChange)
{
storage.Add(this.Text);
}
}
public void Undo()
{
if (storage.Count > 0)
{
this.ignoreChange = true;
this.Text = storage[storage.Count - 1];
storage.RemoveAt(storage.Count - 1);
this.ignoreChange = false;
}
}
}
每次你需要撤消只是打电话:
historyTextBox1.Undo();
该课程将为您提供多个历史记录。
我扩展了Geregor和Keyboard的答案。我无法让代码在Visual Studio 2017中发布,因此进行了一些小调整。然后我也想要一个Redo函数,所以我添加了几行代码。这似乎在我的测试中工作得很好,我想我会分享,因为它回答了问题并进行了扩展。
此代码添加了undo,redo,您可以按住Ctrl-Z或Ctrl-Y使其“运行”通过列表。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace CodeBuilder
{
public class HistoryTextBox : System.Windows.Forms.TextBox
{
bool ignoreChange = true;
List<string> storageUndo = null;
List<string> storageRedo = null;
protected override void OnCreateControl()
{
base.OnCreateControl();
storageUndo = new List<string>();
storageRedo = new List<string>();
ignoreChange = false;
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
if (!ignoreChange)
{
this.ClearUndo();
if (storageUndo.Count > 2048) storageUndo.RemoveAt(0);
if (storageRedo.Count > 2048) storageRedo.RemoveAt(0);
storageUndo.Add(this.Text);
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Z && (ModifierKeys & Keys.Control) == Keys.Control)
{
this.ClearUndo();
ignoreChange = true;
this.Undo();
ignoreChange = false;
}
else if (e.KeyCode == Keys.Y && (ModifierKeys & Keys.Control) == Keys.Control)
{
this.ClearUndo();
ignoreChange = true;
this.Redo();
ignoreChange = false;
}
else
{
base.OnKeyDown(e);
}
}
public void Redo()
{
if (storageRedo.Count > 0)
{
this.ignoreChange = true;
this.Text = storageRedo[storageRedo.Count - 1];
storageUndo.Add(this.Text);
storageRedo.RemoveAt(storageRedo.Count - 1);
this.ignoreChange = false;
}
}
public new void Undo()
{
if (storageUndo.Count > 0)
{
this.ignoreChange = true;
storageRedo.Add(this.Text);
this.Text = storageUndo[storageUndo.Count - 1];
storageUndo.RemoveAt(storageUndo.Count - 1);
this.ignoreChange = false;
}
}
}
}