I need my undo/redo code to function like the undo/redo features found in Microsoft Notepad, Microsoft Word etc. (I am creating a Word Processor).
Currently, my Undo and Redo code is working to an extent . But there are a number of issues with the undo/redo functions in my application. These are as follows:
If I type a sentence, such as "Hello, my name is Toby.", and then I undo the text (leaving Hello still on the document), I can only redo up to "is".
If I undo all the text in the document, I cannot redo any of the text. So if I again type "Hello, my name is Toby.", and then I undo that line, I cannot then redo any of the text.
I was hoping somebody could help me rectify these issues.
As of now, the code used by both Undo and redo is as follows:
public struct UndoSection
{
public string Undo;
public int Index;
public UndoSection(int index, string undo)
{
Index = index;
Undo = undo;
}
private void richTextBoxPrintCtrl1_TextChanged(object sender, EventArgs e)
{
{
OldLength = richTextBoxPrintCtrl1.Text.Length; System.Text.RegularExpressions.MatchCollection wordColl = System.Text.RegularExpressions.Regex.Matches(richTextBoxPrintCtrl1.Text, "'?([a-zA-z'-]+)'?");
counter.Text = wordColl.Count.ToString();
}
try
{
if (IsRedoUndo == false && (richTextBoxPrintCtrl1.Text.Substring(richTextBoxPrintCtrl1.Text.Length - 1, 1) == " " || richTextBoxPrintCtrl1.Text.Substring(richTextBoxPrintCtrl1.Text.Length - 1, 1) == ","))
{
StackCount = StackCount + 1;
RTBRedoUndo[StackCount] = richTextBoxPrintCtrl1.Text;
}
}
catch { }
}
public string[] RTBRedoUndo;
public int StackCount = 0;
public int OldLength = 0;
public int ChangeToSave = 5;
public bool IsRedoUndo = false;
public void RTBTextChanged()
{
if (richTextBoxPrintCtrl1.TextLength - OldLength >= ChangeToSave | richTextBoxPrintCtrl1.TextLength - OldLength <= ChangeToSave)
{
StackCount += 1;
RTBRedoUndo[StackCount] = richTextBoxPrintCtrl1.Text;
}
The undo code:
public void UndoCode()
{
IsRedoUndo = true;
if (StackCount > 0 && RTBRedoUndo[StackCount - 1] != null)
{
StackCount = StackCount - 1;
richTextBoxPrintCtrl1.Text = RTBRedoUndo[StackCount];
}
}
The redo code:
public void RedoCode()
{
if (IsRedoUndo == false && richTextBoxPrintCtrl1.Text.Substring(richTextBoxPrintCtrl1.Text.Length - 1, 1) == " ")
IsRedoUndo = true;
if (StackCount > 0 && RTBRedoUndo[StackCount + 1] != null)
{
StackCount = StackCount + 1;
richTextBoxPrintCtrl1.Text = RTBRedoUndo[StackCount];
}
I'm not too sure what to try, so I was hoping that somebody with more of a knowledge of programming could help me, as I'm still a novice and in my learning stages.
Thanks :o)
You seem to be setting your stack count back to zero once you Undo. Thats why when u Redo it goes only up to the amount allowed -1 try
Undo Code
public void UndoCode()
{
IsRedoUndo = true;
if (StackCount > 0 && RTBRedoUndo[StackCount] != null)
{
StackCount = StackCount - 1;
richTextBoxPrintCtrl1.Text = RTBRedoUndo[StackCount+1];
}
}
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.