Next Post >>
<< Previous Post



Posted by Michael Horvath under Technology  PRESS News  on Sep 10 2021

While working on the editor for PRESS Advanced Website Learner script editing we attempted to use the rich text box Undo and Redo capabilities but found that these did not work correctly due to the way the text changed event handler method performed syntax highlighting for the editor. We decided to disable the rich text box Undo and Redo and create our own.

The following code shows the methods for Undo and Redo. These make use of lists that are defined as private members of the Window class of the script editor window. The text changed event handler adds text for each change to the undoStack list.

        private void Undo_Click(object sender, RoutedEventArgs e)
        {
            richTextBox.TextChanged -= richTextBox_TextChanged;

            var undoArray = undoStack.ToArray();

            if (undoStack.Count > 1)
            {
                TextRange txt = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

                if (redoStack != null)
                {
                    redoStack.Add(txt.Text);
                }

                txt.Text = "";

                richTextBox.AppendText(undoArray[undoArray.GetUpperBound(0)-1]);

                txt.ApplyPropertyValue(TextElement.FontFamilyProperty, rtbFontFamily);
                txt.ApplyPropertyValue(TextElement.FontSizeProperty, MyStringExtensions.RTB_FontSize);

                var blocks = richTextBox.Document.Blocks;

                foreach (var block in blocks)
                {
                    Paragraph p = block as Paragraph;
                    p.Margin = new Thickness(0);
                    p.FontSize = MyStringExtensions.RTB_FontSize;
                    p.LineHeight = 1;
                }

                undoStack.RemoveAt(undoStack.Count - 1);

                var eChangeArgs = new TextChangedEventArgs(e.RoutedEvent, UndoAction.None);

                richTextBox_TextChanged((object)richTextBox, eChangeArgs);
            }
            else
            {
                MessageBox.Show("Nothing left to undo.", "No Changes Left", MessageBoxButton.OK, MessageBoxImage.Information);
            }

            richTextBox.TextChanged += richTextBox_TextChanged;
        }

        private void Redo_Click(object sender, RoutedEventArgs e)
        {
            richTextBox.TextChanged -= richTextBox_TextChanged;

            var redoArray = redoStack.ToArray();

            if (redoStack.Count > 0)
            {
                TextRange txt = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                txt.Text = "";

                richTextBox.AppendText(redoArray[redoArray.GetUpperBound(0)]);

                txt.ApplyPropertyValue(TextElement.FontFamilyProperty, rtbFontFamily);
                txt.ApplyPropertyValue(TextElement.FontSizeProperty, MyStringExtensions.RTB_FontSize);

                var blocks = richTextBox.Document.Blocks;

                foreach (var block in blocks)
                {
                    Paragraph p = block as Paragraph;
                    p.Margin = new Thickness(0);
                    p.FontSize = MyStringExtensions.RTB_FontSize;
                    p.LineHeight = 1;
                }

                redoStack.RemoveAt(redoStack.Count - 1);

                var eChangeArgs = new TextChangedEventArgs(e.RoutedEvent, UndoAction.None);

                richTextBox_TextChanged((object)richTextBox, eChangeArgs);

                txt = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

                if (undoStack != null)
                {
                    undoStack.Add(txt.Text);
                }
            }
            else
            {
                MessageBox.Show("Nothing left to redo.", "No Changes Left", MessageBoxButton.OK, MessageBoxImage.Information);
            }

            richTextBox.TextChanged += richTextBox_TextChanged;
        }
   

Tagged --no tags--
share the love!



comments

there are no comments for this post. be the first to post one!


post your comment

name (*) - required
email address (*) - required
site address
your comment (*) - required

(*) - required


Next Post >>
<< Previous Post