Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:无法撤消插入的文本_C#_Winforms_Undo_Redo - Fatal编程技术网

C#:无法撤消插入的文本

C#:无法撤消插入的文本,c#,winforms,undo,redo,C#,Winforms,Undo,Redo,我使用按键事件在自定义RichTextBox中以编程方式添加文本: SelectedText = e.KeyChar.ToString(); 问题是以这种方式插入文本不会触发CanUndo标志 因此,当我尝试撤消/重做文本(通过调用textbox的Undo()和Redo()方法)时,什么也没有发生 我尝试通过编程从TextChanged()事件中调用KeyUp()事件,但仍然没有将CanUndo标记为true 如何撤消插入的文本而不必创建撤消和重做操作列表 谢谢这只是一个想法,但是如果您将插

我使用按键事件在自定义RichTextBox中以编程方式添加文本:

SelectedText = e.KeyChar.ToString(); 
问题是以这种方式插入文本不会触发CanUndo标志

因此,当我尝试撤消/重做文本(通过调用textbox的Undo()和Redo()方法)时,什么也没有发生

我尝试通过编程从TextChanged()事件中调用KeyUp()事件,但仍然没有将CanUndo标记为true

如何撤消插入的文本而不必创建撤消和重做操作列表


谢谢

这只是一个想法,但是如果您将插入符号的位置设置为插入文本的位置,而不是修改文本属性,只是


肯定会有一些怪癖,但正如我所说,这只是一个想法。

我最终决定使用堆栈创建自己的撤消重做系统

下面是我如何做到这一点的简要概述:

private const int InitialStackSize = 500;    
private Stack<String> undoStack = new Stack<String>(InitialStackSize);
private Stack<String> redoStack = new Stack<String>(InitialStackSize); 

private void YourKeyPressEventHandler(...)
{
        // The user pressed on CTRL - Z, execute an "Undo"
        if (e.KeyChar == 26)
        {
            // Save the cursor's position
            int selectionStartBackup = SelectionStart;

            redoStack.Push(Text);
            Text = undoStack.Pop();

            // Restore the cursor's position
            SelectionStart = selectionStartBackup;
        }
        // The user pressed on CTRL - Y, execute a "Redo"
        if (e.KeyChar == 25)
        {
            if (redoStack.Count <= 0)
                return;

            // Save the cursor's position
            int selectionStartBackup = SelectionStart + redoStack.ElementAt(redoStack.Count - 1).Length;

            undoStack.Push(Text);
            Text = redoStack.Pop();

            // Restore the cursor's position
            SelectionStart = selectionStartBackup;

            return;
        }    

        undoStack.Push(Text);
        SelectedText = e.KeyChar.ToString();  
}
private const int InitialStackSize=500;
私有堆栈undoStack=新堆栈(InitialStackSize);
私有堆栈redoStack=新堆栈(InitialStackSize);
私有void YourKeyPressEventHandler(…)
{
//用户按下CTRL-Z,执行“撤消”
如果(e.KeyChar==26)
{
//保存光标的位置
int selectionStartBackup=SelectionStart;
redoStack.Push(文本);
Text=undoStack.Pop();
//恢复光标的位置
SelectionStart=SelectionStart备份;
}
//用户按下CTRL-Y,执行“重做”
如果(e.KeyChar==25)
{
如果(redoStack.Count您可以使用。中的文档说“在不清除撤消缓冲区的情况下将所选文本设置为指定文本”。这似乎令人困惑。我刚刚尝试过,它按预期设置了撤消


尽管它的名称与剪贴板没有任何关系,但它只是将当前选定的文本替换为您作为参数提供的文本,因此似乎只是以非常简单的方式执行问题要求的操作。

谢谢,但出于某种原因,这仍然没有触发CanUndo标志。
private const int InitialStackSize = 500;    
private Stack<String> undoStack = new Stack<String>(InitialStackSize);
private Stack<String> redoStack = new Stack<String>(InitialStackSize); 

private void YourKeyPressEventHandler(...)
{
        // The user pressed on CTRL - Z, execute an "Undo"
        if (e.KeyChar == 26)
        {
            // Save the cursor's position
            int selectionStartBackup = SelectionStart;

            redoStack.Push(Text);
            Text = undoStack.Pop();

            // Restore the cursor's position
            SelectionStart = selectionStartBackup;
        }
        // The user pressed on CTRL - Y, execute a "Redo"
        if (e.KeyChar == 25)
        {
            if (redoStack.Count <= 0)
                return;

            // Save the cursor's position
            int selectionStartBackup = SelectionStart + redoStack.ElementAt(redoStack.Count - 1).Length;

            undoStack.Push(Text);
            Text = redoStack.Pop();

            // Restore the cursor's position
            SelectionStart = selectionStartBackup;

            return;
        }    

        undoStack.Push(Text);
        SelectedText = e.KeyChar.ToString();  
}