5

我正在为 TextBox 开发一个非常简单的撤消功能,但遇到了一个奇怪的问题。当我尝试从Stack包含所有更改的字符串中获取字符串并将它们放入文本框中时,我看不到任何更改。

我做了一个小调试标签来检查这是否真的有效。我发现它在标签中工作,但在文本框中它使用自己的撤消功能。

有没有办法取消或覆盖文本框撤消并使用我自己的功能?

这是我所做更改的示例代码:

 private void Form1_KeyDown(object sender, KeyEventArgs e)
        if (e.KeyCode == Keys.Z && (ModifierKeys & Keys.Control) == Keys.Control)
            {
                nameTextBox.Text = undoName.GetLastChange(); //--> not working

                undoDebuglabel.Text = undoName.GetLastChange(); --> working
            }
}

GetLastChange() 正在从Stack类内部获取信息。

就像文本框不让我看到更改一样。可能是因为我使用相同的快捷方式CTRL + Z来做到这一点?

4

3 回答 3

4

使用ClearUndo方法清除文本框自己的堆栈。尝试这个:

nameTextBox.ClearUndo();
nameTextBox.Text = undoName.GetLastChange();
于 2012-10-20T17:39:00.037 回答
3

You can create your own TextBox to handle history by inheriting from System.Windows.Forms.TextBox. Take a look at my sample:

public class HistoryTextBox: System.Windows.Forms.TextBox
{
    bool ignoreChange = false;
    List<string> storage = null;


    protected override void OnCreateControl()
    {
        base.OnCreateControl();
        //init storage...
        storage = new List<string>();
    }

    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        //save change to storage...
        if (!ignoreChange)
        {
            storage.Add(this.Text);
        }
    }

    public void Undo()
    {
        if (storage.Count > 0)
        {
            this.ignoreChange = true;
            this.Text = storage[storage.Count - 1];
            storage.RemoveAt(storage.Count - 1);
            this.ignoreChange = false;
        }
    }
}

Everytime you need to undo just call:

historyTextBox1.Undo();

This class will give you multiple histoy records.

于 2012-10-20T17:49:41.200 回答
2

我扩展了GeregorKeyboard的答案。我无法让代码像 Visual Studio 2017 中发布的那样工作,所以做了一个小的调整。然后我也想要一个重做功能,所以我添加了几行代码。这在我的测试中似乎工作正常,我想我会分享,因为它回答了问题并扩展了。

此代码添加了撤消、重做,您可以按住 Ctrl-Z 或 Ctrl-Y 让它在列表中“运行”。

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CodeBuilder
{
    public class HistoryTextBox : System.Windows.Forms.TextBox
    {
        bool ignoreChange = true;
        List<string> storageUndo = null;
        List<string> storageRedo = null;


        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            storageUndo = new List<string>();
            storageRedo = new List<string>();
            ignoreChange = false;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            if (!ignoreChange)
            {
                this.ClearUndo();
                if (storageUndo.Count > 2048) storageUndo.RemoveAt(0);
                if (storageRedo.Count > 2048) storageRedo.RemoveAt(0);

                storageUndo.Add(this.Text);
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Z && (ModifierKeys & Keys.Control) == Keys.Control)
            {
                this.ClearUndo();
                ignoreChange = true;
                this.Undo();
                ignoreChange = false;
            }
            else if (e.KeyCode == Keys.Y && (ModifierKeys & Keys.Control) == Keys.Control)
            {
                this.ClearUndo();
                ignoreChange = true;
                this.Redo();
                ignoreChange = false;
            }
            else
            {
                base.OnKeyDown(e);
            }
        }

        public void Redo()
        {
            if (storageRedo.Count > 0)
            {
                this.ignoreChange = true;
                this.Text = storageRedo[storageRedo.Count - 1];
                storageUndo.Add(this.Text);
                storageRedo.RemoveAt(storageRedo.Count - 1);
                this.ignoreChange = false;
            }
        }

        public new void Undo()
        {
            if (storageUndo.Count > 0)
            {
                this.ignoreChange = true;
                storageRedo.Add(this.Text);
                this.Text = storageUndo[storageUndo.Count - 1];
                storageUndo.RemoveAt(storageUndo.Count - 1);
                this.ignoreChange = false;
            }
        }
    }
}
于 2018-03-06T14:38:52.050 回答