[英]RichTextBox Undo Adding Spaces
我为RichTextBox创建了自己的撤消系统,通过该系统,每当您执行某项操作时,都会将撤消操作添加到堆栈中,而当您按undo时,将撤消该操作。
此行为与我为之实现的所有控件(RichTextBoxes除外)完美配合。 我将系统简化为最简单的元素,每当您按Delete键时,它都会将当前选定的文本及其索引添加到堆栈中,而在您撤消此操作时,它将文本重新放置在该索引处。
这是去除了最简单元素的代码(例如文本文件的实际读取):
// Struct I use to store undo data
public struct UndoSection
{
public string Undo;
public int Index;
public UndoSection(int index, string undo)
{
Index = index;
Undo = undo;
}
}
public partial class Form1 : Form
{
// Stack for holding Undo Data
Stack<UndoSection> UndoStack = new Stack<UndoSection>();
// If delete is pressed, add a new UndoSection, if ctrl+z is pressed, peform undo.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.None && e.KeyCode == Keys.Delete)
UndoStack.Push(new UndoSection(textBox1.SelectionStart, textBox1.SelectedText));
else if (e.Control && e.KeyCode == Keys.Z)
{
e.Handled = true;
UndoMenuItem_Click(textBox1, new EventArgs());
}
}
// Perform undo by setting selected text at stored index.
private void UndoMenuItem_Click(object sender, EventArgs e)
{
if (UndoStack.Count > 0)
{
// Save last selection for user
int LastStart = textBox1.SelectionStart;
int LastLength = textBox1.SelectionLength;
UndoSection Undo = UndoStack.Pop();
textBox1.Select(Undo.Index, 0);
textBox1.SelectedText = Undo.Undo;
textBox1.Select(LastStart, LastLength);
}
}
}
但是,如果仅从一行中选择\\ n,则在下面选择更多文本,如下所示: ,然后按Delete键,然后再撤消,似乎两次撤消了该\\ n字符。
我已经设置了这段代码,它似乎可以通过文本框和RichTextBox为您完成我想做的事情,但我无法删除或添加多余的空间。 是否可以按照特定的操作顺序尝试重新创建您的问题?
我想我已经解决了。 当您突出显示这样的文本时: 您还要在我指向的最后一行的末尾添加\\ n字符。 但是,当您按Delete键时,实时出价实际上不会删除此字符。 因此,当您撤消删除操作时,您必须删除所有尾随\\ n字符,因为它们实际上并没有被删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.