RichTextBox 撤消添加空格

发布于 2024-10-03 16:05:58 字数 1791 浏览 0 评论 0原文

我为 RichTextBox 创建了自己的撤消系统,每当您执行某些操作时,撤消操作就会添加到堆栈中,并且当您按撤消键时,该操作将被撤消。

此行为与我实现它的所有控件完美配合,除了 RichTextBox。我已将系统简化为最简单的元素,每当您按删除键时,它都会将当前选定的文本及其索引添加到堆栈中,当您撤消此操作时,它会将文本放回到该索引处。

以下是删除了最简单元素的代码(如文本文件的实际读取):

// 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,以及下面的更多文本,如下所示: alt text,然后按delete,然后undo,好像把这个\n字符undo了两次。

I've created my own undo system for the RichTextBox whereby whenever you do something an undo action is added to a stack, and when you press undo, this action is undone.

This behavior works perfectly with all controls I've implemented it for, except for RichTextBoxes. I have reduced the system down to its simplest elements, where whenever you press delete, it adds the current selected text and its index to a stack, and when you undo this, it puts the text back at this index.

Here is the code with the simplest elements stripped out (like the actual reading of the text file):

// 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);
        }
    }
}

However if you select just the \n from one line, and more text below like this: alt text, then press delete, and then undo, it seems to undo this \n character twice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨轻弹 2024-10-10 16:05:58

我已经设置了这段代码,它似乎可以通过文本框和 Richtextbox 执行您希望它为我执行的操作,但我无法删除或添加额外的空格。我可以尝试重现您的问题吗?是否有特定的操作顺序?

I've set this code up and it seems to do what you want it to do for me with a textbox and a richtextbox, I was unable to get extra spaces to be removed or added. Is there a specific order of operations that I could try to recreate your issue?

谁许谁一生繁华 2024-10-10 16:05:58

我想我已经解决了。当您突出显示如下文本时: alt text 您还可以在我的最后一行的末尾添加 \n 字符我们指出。然而,当您按下删除键时,RTB 实际上并没有删除该角色。因此,当您撤消删除时,必须删除所有尾随 \n 字符,因为它们实际上并未被删除。

I think I've worked it out. When you highlight text like this: alt text you also include the \n character at the end of the last line that I've pointed to. However when you press delete, the RTB doesn't actually delete this character. So when you are undoing the delete, you have to remove any trailing \n characters, because they aren't actually being deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文