[英]How to get RichTextBox Undo to work better?
我正在尝试使用RichTextBox
编写文本编辑器。 我现在关心的是RichTextBox的撤消和可能的重做功能。
当我开始在文本框中书写时,说1分钟! 如果我调用Undo
方法,它所做的只是我相信再次清除或重置richtextbox。 我如何才能使其更好地工作,例如撤消上一个添加的单词或最后添加的新行...我指的是您希望从撤消功能中获得的通常的东西。 (重做也同样重要!)
是否有属性或某些选项可以实现? 还是我必须实现自己的代码?
只需继续阅读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;
}
}
您可以保存最新数据,并且要撤消操作时可以将现在数据更改为最新数据! 您可以随时设置最新数据!
我用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,可以在多次撤消保存后删除列表中的第一个数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.