简体   繁体   中英

C# RichTextBox: Highlighting + Undo

I'm doing a simple xhtml editor with syntax highlighting. The problem is that in richtextbox undo history everything is stored. Even my highlighting changes. I already found that there is a richtextbox.UndoActionName which should be "Unknown" if you change the richtextbox programatically. So I tried something like this when ctrl+z is pressed:

while(richtextbox.CanUndo && richtextbox.UndoActionName == "Unknown"){
    richtextbox.Undo();
}

Which just selected my whole text and did not actually undo anything (just kept looping until I stopped it...). So my question is whether I need to specify somewhere the undoactionname myself or whether I can change the richtextbox to record undo history only for typing? Thanks.

Edit: It would be cool if I could send a message to richtextbox to stop recording undo history while i'm highlighting or to somehow exclude or actions with Unknown name, is there any way to do this?

Edit 2: Well I've done it the stupid way, I'm storing the whole text in linked list and when redoing I clear the richtextbox and fill it with last item from the list and then re-highlight. Maybe this helps anyone

Problem is you are trying to undo last action, which will be an undo for next operation, hence it will keep doing that.

Try this:

while(richtextbox.CanUndo) {
  richtextbox.Undo();
  // Clear the undo buffer to prevent last action from being
  richtextbox.ClearUndo();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM