Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, :)

Is there any way to remove actions from the undo buffer?

I have created a basic RichTextBox syntax highlighter. As you type it will search for key words within the line you are currently editing. When a key word is found it will select that word then apply a color to it.

Here is what I mean: I type the words "float boat". The word "float" is a key word and will be selected then it's color changed... "boat" is not a key word and will be left alone. This is the input sequence:

f, l, o, a, t, selection of float, space, b, o, a, t

So, when you undo the typing of the two words all is well until you try to undo the typing of the "t" in float. You have to press undo twice, once to undo the selection of float and once to undo the "t".

All I want to have in the undo buffer is:
f, l, o, a, t, space, b, o, a, t
but not the selection of the word.

Any ideas will be greatly appreciated!

~Rich~
Posted
Updated 1-Jan-11 11:01am
v2
Comments
Sergey Alexandrovich Kryukov 1-Jan-11 15:41pm    
How undo is related to clipboard? Normally, undo does not use clipboard. In your case, at what moment the data is copied to the clipboard?
Manfred Rudolf Bihy 1-Jan-11 17:02pm    
Edited to remove clipboard reference as it has nothing to do with OP's problem. Adjusted title for conciseness.
Manfred Rudolf Bihy 1-Jan-11 17:12pm    
Update: See my comment on my answer.

1 solution

As SAKryukov stated in his comment this has nothing to do with the clipboard. After selecting the key word in your text and applying the styling to it call ClearUndo(). This clears the most recent operation from the undo buffer which should then be the selection of the word.

Best Regards,

Manfred
 
Share this answer
 
Comments
programmer095 1-Jan-11 16:42pm    
I guess it should work. I guess Clipboard really was misleading. My 5
Sergey Alexandrovich Kryukov 1-Jan-11 16:50pm    
I think I should agree (5 from me, too)
junrall 1-Jan-11 17:00pm    
I've tried using ClearUndo before and just now tried it again in a basic project... it seems to clear the entire clipboard. I created a richtextbox then added a button that handles the ClearUndo.
I pasted some text twice into the richtextbox then clicked the button. When I look at RichTextBox1.UndoActionName there is no undo to perform. I would think that the first paste action should still be there... am I nut understanding how the ClearUndo works? Or am I using it incorrectly?

Thanks!
Manfred Rudolf Bihy 1-Jan-11 17:09pm    
Hi Rich, please got to this site:
http://dotnetperls.com/undo-paste
They're doing it with a TextBox but the same should apply to the RichTextBox. The problem is the paste action from the clipboard which resets the undo buffer. They explain how this can be avoided with some code. It is C# but isn't exactly rocket science and you should have no problem to adapt it to VB.NET.
junrall 1-Jan-11 17:36pm    
Thanks Manfred,
I took a look at that link... unfortunately, I don't think that is exactly what I need.
I did come up with a work around that seems to work:
Public Sub UndoText()
While RichTextBox1.UndoActionName = "Unknown"
Me.Undo()
End While
RichTextBox1.Undo()
End Sub

The "while" will undo any and all selections prior to the last typed letter or whatever was done (paste, delete, enter, ect.). Then one last undo removes the last user action.
Works like a charm!
Have you seen programs that have a dropdown that lists all of the undo actions? When you select an undo item in the list the program will undo everthing up to

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900