问题描述
我一直在试图获得的撤消和重做在我的文本编辑器现在工作了约3天。它做我的头
I've been trying to get undo and redo working in my text editor for about 3 days now. It's doing my head in.
我有一个文本框(名为 richTextBoxPrintCtrl1
),我想是能够为撤消并*的重做* 的(一字一句)。
I have a text box (named richTextBoxPrintCtrl1
), which I would like to be able to undo and *redo *(word by word).
所以,如果我点击撤消按钮,它的撤消的硬道理。如果我再单击重做按钮,它的重做的最后一个字。
So if I click the undo button, it undoes the last word. And if I then clicked the redo button, it redoes the last word.
可能有人帮助我得到这个加工?
Could somebody help me get this working?
richTextBoxPrintCtrl1.Undo();
不工作得很好。它删除一切都在文本框中键入
richTextBoxPrintCtrl1.Undo();
doesn't work very well. It deletes everything typed in the text box.
在此先感谢您的帮助。
我知道这个问题之前已经问了很多次,但我不能得到它的工作使用从我在这里浏览上这样的问题的信息。
I know this question has been asked many times before, but I can't get it working using the information from the questions I've browsed here on SO.
推荐答案
好吧,我会后一些代码,以帮助您开始。首先你要听的框TextChanged
事件。
Ok, I'll post some code to help you get started. first you need to listen for the TextChanged
event.
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
和你需要在类
Stack<string> undoList = new Stack<string>();
在文本改变了处理程序,您需要将文本框的内容添加到堆栈中。
In the text changed handler, you need to add the contents of the textbox to the stack
void textBox1_TextChanged(object sender, EventArgs e)
{
undoList.Push(textBox1.Text);
}
然后,你需要处理撤消,所以你可以简单地使用CTRL-Z
Then you need to handle the undo, so you could simply use CTRL-Z
textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Z && (e.Control)) {
textBox1.Text = undoList.Pop();
}
}
这篇关于如何撤消,并在C#重做(富文本框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!