C# 在 Windows 窗体应用程序中撤消

发布于 2024-10-08 15:31:12 字数 195 浏览 0 评论 0原文

我想知道在 C# 中开发撤消的简单方法是什么

我有一个 Windows 表单应用程序,在窗口中我有一个带有很多文本框、选择框、复选框...

用户填写并单击后的表单在计算按钮上, 然后程序根据所有输入进行计算并将一个值返回到屏幕。

我寻找一种简单的方法来在每次计算后保存状态,并让用户能够返回一个或多个步骤。

谢谢

I want to know what is the easy way to develop an undo in c#

i have a windows form application, in the window i have a form with a lot of text box , select box , checkbox ...

after the user fill in and click on calculate button,
then the program calculate according to all the inputs and return a value to the screen.

i search an easy way to save the states after every calculte , and give the user the abbility to return one, or more steps back .

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多情出卖 2024-10-15 15:31:12

我认为没有任何标准的内置功能,但您可以找到一些框架来自己完成此操作: 撤消框架,包含表单示例:撤消框架示例

还有受监控的撤消框架

I don't think there is any standard built-in, but you can find some frameworks to do it yourself: The Undo Framework with a sample for Forms: Samples for the Undo Framework

There is also the Monitored Undo Framework.

捂风挽笑 2024-10-15 15:31:12

一个简单的实现是堆栈。当用户执行操作时,将当前值压入堆栈。当它们“撤消”时,将从堆栈中弹出值。

A simple implementation would be a stack. As the user performs operations, push the current value onto the stack. As they 'undo', pop values off of the stack.

清秋悲枫 2024-10-15 15:31:12

最简单的方法是将输入文本保存在某处。

您可以通过某种方式仅编码自上次保存输入以来已更改的文本来获得更多奇特的效果。例如,您可以仅存储添加的新文本及其添加位置。或者您可以仅存储已删除文本的位置和长度。但这更复杂,而且效果是一样的。

虽然文本框确实具有内置的简单撤消功能,但如果这对您不起作用,您只需在每个撤消点保存文本即可。这很容易、简单而且必要。

The easiest way is to simply save the input text somewhere.

You could get more fancy by encoding, somehow, only the text that has changed from the last time the input was saved. For example, you could store only new text that was added along with the position it was added. Or you could store only the position and length of text that was deleted. But that's more complex and the effect is the same.

While a text box does have a simple undo feature built in, if that doesn't work for you, you'll just have to save the text at each undo point. It's easy, simple, and necessary.

旧街凉风 2024-10-15 15:31:12

如果您使用的是文本框。 向后撤消 1 步

您可以使用 `Undo() 方法 n txtBoxResult.Undo();

If you are using Textbox. You can Undo it by 1 step back by using `Undo() method n

txtBoxResult.Undo();

夕嗳→ 2024-10-15 15:31:12

已经很晚了,但昨天刚刚收到朋友的询问。

我在 richtextbox 中开发了这个简单的撤消重做功能。是的,有很多更好的方法,但它只是作为一种快速解决方案可以使用。

using System;
using System.Windows.Forms;

namespace Undo_Redo
{
    public partial class Form1 : Form
    {
        public string[] RTBRedoUndo;
        public int StackCount = -1;
        public bool IsRedoUndo = false;
        public string[] RTBRedo;
        public int RedoStackCount = -1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RTBRedoUndo = new string[10000];
            RTBRedo = new string[10000];

        }
        private void btn_undo_Click(object sender, EventArgs e)
        {

            if (StackCount > -1 && RTBRedoUndo[StackCount] != null)
            {
                IsRedoUndo = true;
                RedoStackCount += 1;
                RTBRedo[RedoStackCount] = RTBRedoUndo[StackCount];
                richTextBox1.Text = richTextBox1.Text.Substring(0, richTextBox1.Text.Length - 1);
                RTBRedoUndo[StackCount] = null;
                StackCount -= 1;

            }
        }

        private void btn_redo_Click(object sender, EventArgs e)
        {
            if (RedoStackCount > -1 && RTBRedo[RedoStackCount] != null)
            {
                IsRedoUndo = true;
                StackCount += 1;
                RTBRedoUndo[StackCount] = RTBRedo[RedoStackCount];
                richTextBox1.Text = richTextBox1.Text + RTBRedo[RedoStackCount];
                RTBRedo[RedoStackCount] = null;
                RedoStackCount -= 1;
            }
        }

        private void btn_redo_MouseUp(object sender, MouseEventArgs e)
        {
            IsRedoUndo = false;
        }

        private void btn_undo_MouseUp(object sender, MouseEventArgs e)
        {
            IsRedoUndo = false;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (IsRedoUndo == false)
            {
                StackCount += 1;
                RTBRedoUndo[StackCount] = richTextBox1.Text.Substring(richTextBox1.Text.Length - 1, 1);
            }
        }
    }
}

Its pretty late, but just got a query from friend yesterday.

I developed this simple undo redo functionality in richtextbox. Yes there are many better ways but just as a quick solution it can be used.

using System;
using System.Windows.Forms;

namespace Undo_Redo
{
    public partial class Form1 : Form
    {
        public string[] RTBRedoUndo;
        public int StackCount = -1;
        public bool IsRedoUndo = false;
        public string[] RTBRedo;
        public int RedoStackCount = -1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RTBRedoUndo = new string[10000];
            RTBRedo = new string[10000];

        }
        private void btn_undo_Click(object sender, EventArgs e)
        {

            if (StackCount > -1 && RTBRedoUndo[StackCount] != null)
            {
                IsRedoUndo = true;
                RedoStackCount += 1;
                RTBRedo[RedoStackCount] = RTBRedoUndo[StackCount];
                richTextBox1.Text = richTextBox1.Text.Substring(0, richTextBox1.Text.Length - 1);
                RTBRedoUndo[StackCount] = null;
                StackCount -= 1;

            }
        }

        private void btn_redo_Click(object sender, EventArgs e)
        {
            if (RedoStackCount > -1 && RTBRedo[RedoStackCount] != null)
            {
                IsRedoUndo = true;
                StackCount += 1;
                RTBRedoUndo[StackCount] = RTBRedo[RedoStackCount];
                richTextBox1.Text = richTextBox1.Text + RTBRedo[RedoStackCount];
                RTBRedo[RedoStackCount] = null;
                RedoStackCount -= 1;
            }
        }

        private void btn_redo_MouseUp(object sender, MouseEventArgs e)
        {
            IsRedoUndo = false;
        }

        private void btn_undo_MouseUp(object sender, MouseEventArgs e)
        {
            IsRedoUndo = false;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (IsRedoUndo == false)
            {
                StackCount += 1;
                RTBRedoUndo[StackCount] = richTextBox1.Text.Substring(richTextBox1.Text.Length - 1, 1);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文