c# - How can I implement effective Undo/Redo into my WinForms application? -


i have winform text editor.

i able allow user undo , redo changes in rich text box, can in microsoft word.

i have spent past week or researching how this, , results seem regarding graphics applications.

the standard richtextbox1.undo(); gives disappointing results, undoes user has written.

does have idea how implement effective undo/redo? preferably 1 undoes/redoes action word-by-word opposed character-by-character.

this basic idea, , i'm sure many improvements made.

i create string array , incrementally store value of richtextbox (in textchanged event, under own conditions) in array. store value, increment value of counter, stackcount. when user undoes, decrement stackcount , set richtextbox.text = array(stackcount). if redo, increment value of counter , set value again. if undo , change text, clear values onwards.

i sure many other people may have better suggestions/changes this, please post in comments , update, or edit yourself!

example in c#

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace redoundoapp { public partial class form1 : form {     public form1()     {         initializecomponent();     }      public string[] rtbredoundo;     public int stackcount = 0;     public int oldlength = 0;     public int changetosave = 5;     public bool isredoundo = false;      private void form1_load(object sender, eventargs e)     {         rtbredoundo = new string[10000];         rtbredoundo[0] = "";     }      private void undo_click(object sender, eventargs e)     {         isredoundo = true;         if (stackcount > 0 && rtbredoundo[stackcount - 1] != null)         {             stackcount = stackcount - 1;             richtextbox1.text = rtbredoundo[stackcount];         }     }      private void redo_click(object sender, eventargs e)     {         isredoundo = true;         if (stackcount > 0 && rtbredoundo[stackcount + 1] != null)         {             stackcount = stackcount + 1;             richtextbox1.text = rtbredoundo[stackcount];         }      }      private void richtextbox1_textchanged(object sender, eventargs e)     {         if (isredoundo == false && richtextbox1.text.substring(richtextbox1.text.length - 1, 1) == " ")//(math.abs(richtextbox1.text.length - oldlength) >= changetosave && isredoundo == false)         {             stackcount = stackcount + 1;             rtbredoundo[stackcount] = richtextbox1.text;             oldlength = richtextbox1.text.length;         }     }      private void undo_mouseup(object sender, mouseeventargs e)     {         isredoundo = false;     }      private void redo_mouseup(object sender, mouseeventargs e)     {         isredoundo = false;     } } } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -