Windows Forms TextChanged

I am trying to make simple Windows Forms application. I write text to TextBox, and every time I do it, text is pushed to the list, so we have all text conditions. And so we have "BACK" button to use it. buttonBack.Click works well... But box.TextChanged not. I want this event to be called each time I change the text

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

Upvotes: 0

Views: 550

Answers (1)

David Fletcher
David Fletcher

Reputation: 305

I believe that it is not working because when you update the text from the "back" button, it is adding that to the stack so that it appears that the back button isn't working, but it really is. You simply need to evaluate if the text change should be added to the stack or not. Here is an example.

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

I also changed what was sent back to the text box in your buttonBack.Click event.

Upvotes: 1

Related Questions