如何让RichTextBox Undo更好地工作?

时间:2011-12-29 11:29:02

标签: c# winforms richtextbox undo redo

我正在尝试使用RichTextBox编写文本编辑器。我现在关注的是关于RichTextBox的Undo和Redo功能。

当我开始在文本框中书写时,说1分钟!如果我调用Undo方法,它所做的只是我相信清除或重置richtextbox。我怎样才能使它能够做得更好,比如撤消上一个添加的单词,或者上一个添加的新行......我的意思是你希望从撤销功能中获得通常的东西。 (同样重要的是Redo!)

是否有属性或某些选项可以实现这一目标?或者我必须实现自己的代码?

2 个答案:

答案 0 :(得分:3)

从ahmadali的代码开始 - 你可以把它放到一个单独的类中,并实现重做功能:

NB。是的,它会在每次更改文本框时保存所有文本,因此如果您的应用程序将用于大量文本或者应用程序将在较长时间内(即数天/周)打开,则可以更改该文本

public partial class MainForm : Form
{

    Undoer undoer;

    public MainForm()
    {
        InitializeComponent();

        this.txtBox.TextChanged += new EventHandler( TextBoxTextChanged );
        this.undoer = new Undoer(ref this.txtText);


        // create a context menu
        ContextMenu menu = new ContextMenu();
        menu.MenuItems.AddRange( new MenuItem[] { 
                    new MenuItem("&Undo",  new EventHandler( this.undoer.undo_Click  )),
                    new MenuItem("&Redo",  new EventHandler( this.undoer.redo_Click  ))
        });
        this.txtBox.ContextMenu = menu;

        // or create keypress event 
        this.txtBox.KeyDown += new KeyEventHandler( textBox_KeyDown );
        this.KeyDown  += new KeyEventHandler( textBox_KeyDown );
    }

    protected void TextBoxTextChanged(object sender, EventArgs e)
    {
        undoer.Save();
    }

    protected void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.Modifiers == (System.Windows.Forms.Keys.Control))
        {
            if ( e.KeyCode == Keys.Z )
            {
                this.undoer.Undo();
                e.Handled = true;
            }
            if ( e.KeyCode == Keys.Y )
            {
                this.undoer.Redo();
                e.Handled = true;
            }
        }
    }
}

public class Undoer
{
    protected System.Windows.Forms.RichTextBox txtBox;
    protected List<string> LastData = new List<string>();
    protected int  undoCount = 0;

    protected bool undoing   = false;
    protected bool redoing   = false;


    public Undoer(ref System.Windows.Forms.RichTextBox txtBox)
    {
        this.txtBox = txtBox;
        LastData.Add(txtBox.Text);
    }

    public void undo_Click(object sender, EventArgs e)
    {
        this.Undo();
    }
    public void redo_Click(object sender, EventArgs e)
    {
        this.Redo();
    }

    public void Undo()
    {
        try
        {
            undoing = true;
            ++undoCount;
            txtBox.Text = LastData[LastData.Count - undoCount - 1];
        }
        catch { }
        finally{ this.undoing = false; }
    }
    public void Redo()
    {
        try
        {
            if (undoCount == 0)
                return;

            redoing = true;
            --undoCount;
            txtBox.Text = LastData[LastData.Count - undoCount - 1];
        }
        catch { }
        finally{ this.redoing = false; }
    }

    public void Save()
    {
        if (undoing || redoing)
            return;

        if (LastData[LastData.Count - 1] == txtBox.Text)
            return;

        LastData.Add(txtBox.Text);
        undoCount = 0;
    }
}

答案 1 :(得分:2)

您可以保存最新数据,当您想要撤消时,您可以将数据更改为最后数据!可以随时设置最新数据!

我使用richTextBox创建一个winForm,并使用按钮撤消写入的文本:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test
{


public partial class Form1 : Form
{
    List<string> LastData = new List<string>();

    int undoCount = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        LastData.Add(richTextBox1.Text);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            richTextBox1.Text = LastData[LastData.Count - undoCount - 1];
            ++undoCount;
        }
        catch { }
    }

    private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        LastData.Add(richTextBox1.Text);
        undoCount = 0;
    }
}
}

但我找不到任何更好,更有条理的方式,你可以改变

LastData.Add(richTextBox1.Text);
undoCount = 0;

保存新词或新行

<强>更新 如果你想保存Ram,你可以在许多撤消保存后删除列表中的第一个数据。