问题
I am trying to write a text editor using RichTextBox
. My concern is now about Undo and possibly Redo features of RichTextBox.
When I start writing in text box, say 1 minute! if I call Undo
method, all it does is just I beleive clearing or resetting richtextbox again. How can I get it to work that it can do better, like Undoing last added word, or last added new line...I mean usual things you expect from Undo function. (The same counts for Redo also!)
Is there properties or some options to achive this? Or I have to implement my own code?
回答1:
You can save the lastest Data and when you want to undo you can change to now data to last data! lastest data can be set anytime that you want!
I Make a winForm with a richTextBox and a button that button undo the wrote text:
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;
}
}
}
but I didn't find any better and organized way and you can change
LastData.Add(richTextBox1.Text);
undoCount = 0;
to save new words or new line
update: if you want to save the Ram you can delete the first data on list after many undo saving.
回答2:
Just to go on from ahmadali's code - you can put it into a seperate class, and implement the redo functionality also:
NB. yes, it saves all the text every time the textbox is changed, so you can change that if your app will be used for massive amounts of text or if the app will be open for extended periods (ie days/weeks)
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;
}
}
来源:https://stackoverflow.com/questions/8667302/how-to-get-richtextbox-undo-to-work-better