.NET RichTextBox undo

I use RichTextBox in WinForms 3.5, and I found that when editing the contained text programmatically, these changes are no longer available for the built-in undo functions.

Is there a way to do this so that these changes are available for undo / redo?

+6
c # undo winforms
source share
1 answer

Here are just the code I decided to chat with:

string buffer = String.Empty; string buffer2 = String.Empty; public Form3() { InitializeComponent(); this.richTextBox1.KeyDown += new KeyEventHandler(richTextBox1_KeyDown); this.richTextBox1.TextChanged += new EventHandler(richTextBox1_TextChanged); } void richTextBox1_TextChanged(object sender, EventArgs e) { buffer2 = buffer; buffer = richTextBox1.Text; } void richTextBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.Z) { this.richTextBox1.Text = buffer2; } } private void button1_Click(object sender, EventArgs e) { richTextBox1.Text = "Changed"; } 

Basically I write my own Undo function. All I do is store the old value in one buffer variable and the new value in the second buffer variable. Each time the text changes, these values ​​are updated. Then, if the user presses "CTRL-Z", he replaces the text with the old value. Hack? Small. But it works for the most part.

+2
source share

All Articles