Windows窗体TextChanged

时间:2017-12-17 05:49:09

标签: c# winforms

我正在尝试制作简单的Windows窗体应用程序。我将文本写入TextBox,每次执行此操作时,文本都会被推送到列表中,因此我们拥有所有文本条件。所以我们有“BACK”按钮来使用它。 buttonBack.Click效果很好......但box.TextChanged没有。 我希望每次更改文本时都会调用此事件

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

namespace TextBox
{
    class Revision
    {
        public string Text { get; set; }
        public int CoursorPosition { get; set; }
    }

    class MyForm : Form
    {
        static RichTextBox box = new RichTextBox();
        static List<Revision> revisions = new List<Revision>();

        static void MakeRevision()
        {
            revisions.Add(new Revision
            {
                Text = box.Text,
                CoursorPosition = box.SelectionStart
            });
        }

        public MyForm()
        {

            var buttonBack = new Button()
            {
                Location = new Point(0, 0),
                Size = new Size(ClientSize.Width, 30),
                Text = "Back"
            };

            box.Size = new Size(ClientSize.Width, 100);
            box.Multiline = true;
            box.Location = new Point(0, buttonBack.Bottom);
            box.TextChanged += (sender, args) => MakeRevision();
            box.MouseDown += (sender, args) => MakeRevision();

            Controls.Add(buttonBack);
            Controls.Add(box);

            buttonBack.Click += (sender, args) =>
            {
                box.Text = revisions.Last().ToString();
                revisions.RemoveAt(revisions.IndexOf(revisions.Last()));
            };
        }

        public static void Main()
        {
            var form = new MyForm();
            Application.Run(form);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为它不起作用,因为当您从“后退”按钮更新文本时,它会将其添加到堆栈中,以便看起来后退按钮不起作用,但确实如此。您只需要评估文本更改是否应该添加到堆栈中。这是一个例子。

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

namespace TextBox
{
    class Revision
    {
        public string Text { get; set; }
        public int CoursorPosition { get; set; }
    }

    class MyForm : Form
    {
        static RichTextBox box = new RichTextBox();
        static List<Revision> revisions = new List<Revision>();
        static bool loading = false;

        static void MakeRevision()
        {
            if (loading)
                return;

            revisions.Add(new Revision
            {
                Text = box.Text,
                CoursorPosition = box.SelectionStart
            });
        }

        public MyForm()
        {

            var buttonBack = new Button()
            {
                Location = new Point(0, 0),
                Size = new Size(ClientSize.Width, 30),
                Text = "Back"
            };

            box.Size = new Size(ClientSize.Width, 100);
            box.Multiline = true;
            box.Location = new Point(0, buttonBack.Bottom);
            box.TextChanged += (sender, args) => MakeRevision();
            box.MouseDown += (sender, args) => MakeRevision();

            Controls.Add(buttonBack);
            Controls.Add(box);

            buttonBack.Click += (sender, args) =>
            {
                if (revisions.Count > 0)
                {
                    loading = true;
                    box.Text = revisions.Last().Text;
                    box.SelectionStart = revisions.Last().CoursorPosition;
                    box.Focus();
                    revisions.RemoveAt(revisions.IndexOf(revisions.Last()));
                    loading = false;
                }
            };
        }

        public static void Main()
        {
            var form = new MyForm();
            Application.Run(form);
        }
    }
}

我还更改了发送回buttonBack.Click活动中文本框的内容。