Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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#_Undo - Fatal编程技术网

C# 为什么我的重做&;撤消文本框的方法错误?

C# 为什么我的重做&;撤消文本框的方法错误?,c#,undo,C#,Undo,有人能给我解释一下我做错了什么吗 private Stack<string> undo = new Stack<string>(); private Stack<string> redo = new Stack<string>(); private void undoToolStripMenuItem_Click(object sender, EventArgs e) { insert(); if (textBox1.T

有人能给我解释一下我做错了什么吗

private Stack<string> undo = new Stack<string>();
private Stack<string> redo = new Stack<string>();

private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
         insert();
    if (textBox1.Text !=null)
    {
        redo.Push(textBox1.Text);

        if (undo.Count != 0)
        {
            textBox1.Text = undo.Pop();
        }
    }
}

private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (redo.Count !=0)
    {
        textBox1.Text = redo.Pop();
    }
}

    void insert()
    {
        undo.Push(textBox1.Text);
    }
private Stack undo=new Stack();
私有堆栈重做=新堆栈();
私有void undoToolStripMenuItem\u单击(对象发送方,事件参数e)
{
插入();
如果(textBox1.Text!=null)
{
redo.Push(textBox1.Text);
如果(undo.Count!=0)
{
textBox1.Text=undo.Pop();
}
}
}
private void redoToolStripMenuItem\u单击(对象发送方,事件参数e)
{
如果(重做计数!=0)
{
textBox1.Text=redo.Pop();
}
}
空白插入()
{
撤消.推送(textBox1.Text);
}

此代码不会更改我的文本框。可能问题在于我使用文本框的方式?

何时向撤消或重做堆栈添加项目?每次更改撤消堆栈的文本时,也必须向其添加项。否则,撤消堆栈将始终为空


编辑:在尝试撤消之前将项目添加到撤消堆栈是没有意义的。您必须在文本框的按键或文本更改事件中将项目添加到撤消堆栈中。不要只调用
undoToolStripMenuItem\u单击
中的
insert
方法,也许您应该在
textChanged
事件中调用
insert
方法?您将在
undoToolStripMenuItem\中调用它,第一次单击
。然后您的撤消堆栈有一个元素。然后立即执行此操作:

textBox1.Text = undo.Pop();

在这里,撤消操作有一个元素,即当前文本,您只需替换文本框内容,文本就不会更改。

我忘了添加一个方法。sorry@user3142035编辑我的帖子以回应你的改变。