A
AndyNakamura
Guest
I am using a richtextbox in my application.
I has the facility for bold, underline, font colour etc.
I've been trying to implement Undo/Redo functionality.
I found a solution online. This works ok for pure text however when it comes to undoing/redoing bold, underline font colour changes it doesn't seem to record these changes so does not work for formating.
Anyone know how to implement this sort of functionality too?
Current code below.
Public Class Form1
Private urc As New UndoRedoClass(Of String)()
Private NoAdd As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button_Undo.Enabled = False
Button_Redo.Enabled = False
urc.AddItem(RichTextBox1.Text)
End Sub
Private Sub Button_Undo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Undo.Click
NoAdd = True
urc.Undo()
RichTextBox1.Text = urc.CurrentItem
Button_Undo.Enabled = urc.CanUndo
Button_Redo.Enabled = urc.CanRedo
NoAdd = False
End Sub
Private Sub Button_Redo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Redo.Click
NoAdd = True
urc.Redo()
RichTextBox1.Text = urc.CurrentItem
Button_Undo.Enabled = urc.CanUndo
Button_Redo.Enabled = urc.CanRedo
NoAdd = False
End Sub
Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
If Not NoAdd Then
urc.AddItem(RichTextBox1.Text)
Button_Undo.Enabled = urc.CanUndo
Button_Redo.Enabled = urc.CanRedo
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FontDialog1.ShowDialog()
RichTextBox1.SelectionFont = FontDialog1.Font
End Sub
Private Sub ChkBoxBold_Click(sender As Object, e As EventArgs) Handles ChkBoxBold.Click
Using f = RichTextBox1.SelectionFont
RichTextBox1.SelectionFont = New Font(f, f.Style Xor FontStyle.Bold)
ChkBoxBold.Checked = RichTextBox1.SelectionFont.Bold
End Using
RichTextBox1.Focus()
End Sub
Private Sub ChkBoxItalic_Click(sender As Object, e As EventArgs) Handles ChkBoxItalic.Click
Using f = RichTextBox1.SelectionFont
RichTextBox1.SelectionFont = New Font(f, f.Style Xor FontStyle.Italic)
ChkBoxItalic.Checked = RichTextBox1.SelectionFont.Italic
End Using
RichTextBox1.Focus()
End Sub
Private Sub ChkBoxUnderline_Click(sender As Object, e As EventArgs) Handles ChkBoxUnderline.Click
Using f = RichTextBox1.SelectionFont
RichTextBox1.SelectionFont = New Font(f, f.Style Xor FontStyle.Underline)
ChkBoxUnderline.Checked = RichTextBox1.SelectionFont.Underline
End Using
ChkBoxItalic.Checked = RichTextBox1.SelectionFont.Italic
ChkBoxUnderline.Checked = RichTextBox1.SelectionFont.Underline
Panel1.BackColor = RichTextBox1.SelectionColor
End Sub
Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.SelectionColor = ColorDialog1.Color
Panel1.BackColor = ColorDialog1.Color
'RichTextBox1.SelectionStart = CaretPosn
'RichTextBox1.SelectionLength = 0
End If
RichTextBox1.Focus()
End Sub
Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
ChkBoxBold.Checked = RichTextBox1.SelectionFont.Bold
ChkBoxItalic.Checked = RichTextBox1.SelectionFont.Italic
ChkBoxUnderline.Checked = RichTextBox1.SelectionFont.Underline
Panel1.BackColor = RichTextBox1.SelectionColor
End Sub
End Class
Public Class UndoRedoClass(Of T)
Private UndoStack As Stack(Of T)
Private RedoStack As Stack(Of T)
Public CurrentItem As T
Public Event UndoHappened As EventHandler(Of UndoRedoEventArgs)
Public Event RedoHappened As EventHandler(Of UndoRedoEventArgs)
Public Sub New()
UndoStack = New Stack(Of T)
RedoStack = New Stack(Of T)
End Sub
Public Sub Clear()
UndoStack.Clear()
RedoStack.Clear()
CurrentItem = Nothing
End Sub
Public Sub AddItem(ByVal item As T)
If CurrentItem IsNot Nothing Then UndoStack.Push(CurrentItem)
CurrentItem = item
RedoStack.Clear()
End Sub
Public Sub Undo()
RedoStack.Push(CurrentItem)
CurrentItem = UndoStack.Pop()
RaiseEvent UndoHappened(Me, New UndoRedoEventArgs(CurrentItem))
End Sub
Public Sub Redo()
UndoStack.Push(CurrentItem)
CurrentItem = RedoStack.Pop
RaiseEvent RedoHappened(Me, New UndoRedoEventArgs(CurrentItem))
End Sub
Public Function CanUndo() As Boolean
Return UndoStack.Count > 0
End Function
Public Function CanRedo() As Boolean
Return RedoStack.Count > 0
End Function
Public Function UndoItems() As List(Of T)
Return UndoStack.ToList
End Function
Public Function RedoItems() As List(Of T)
Return RedoStack.ToList
End Function
End Class
Public Class UndoRedoEventArgs
Inherits EventArgs
Private _CurrentItem As Object
Public ReadOnly Property CurrentItem() As Object
Get
Return _CurrentItem
End Get
End Property
Public Sub New(ByVal currentItem As Object)
_CurrentItem = currentItem
End Sub
End Class
Continue reading...
I has the facility for bold, underline, font colour etc.
I've been trying to implement Undo/Redo functionality.
I found a solution online. This works ok for pure text however when it comes to undoing/redoing bold, underline font colour changes it doesn't seem to record these changes so does not work for formating.
Anyone know how to implement this sort of functionality too?
Current code below.
Public Class Form1
Private urc As New UndoRedoClass(Of String)()
Private NoAdd As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button_Undo.Enabled = False
Button_Redo.Enabled = False
urc.AddItem(RichTextBox1.Text)
End Sub
Private Sub Button_Undo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Undo.Click
NoAdd = True
urc.Undo()
RichTextBox1.Text = urc.CurrentItem
Button_Undo.Enabled = urc.CanUndo
Button_Redo.Enabled = urc.CanRedo
NoAdd = False
End Sub
Private Sub Button_Redo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Redo.Click
NoAdd = True
urc.Redo()
RichTextBox1.Text = urc.CurrentItem
Button_Undo.Enabled = urc.CanUndo
Button_Redo.Enabled = urc.CanRedo
NoAdd = False
End Sub
Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
If Not NoAdd Then
urc.AddItem(RichTextBox1.Text)
Button_Undo.Enabled = urc.CanUndo
Button_Redo.Enabled = urc.CanRedo
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FontDialog1.ShowDialog()
RichTextBox1.SelectionFont = FontDialog1.Font
End Sub
Private Sub ChkBoxBold_Click(sender As Object, e As EventArgs) Handles ChkBoxBold.Click
Using f = RichTextBox1.SelectionFont
RichTextBox1.SelectionFont = New Font(f, f.Style Xor FontStyle.Bold)
ChkBoxBold.Checked = RichTextBox1.SelectionFont.Bold
End Using
RichTextBox1.Focus()
End Sub
Private Sub ChkBoxItalic_Click(sender As Object, e As EventArgs) Handles ChkBoxItalic.Click
Using f = RichTextBox1.SelectionFont
RichTextBox1.SelectionFont = New Font(f, f.Style Xor FontStyle.Italic)
ChkBoxItalic.Checked = RichTextBox1.SelectionFont.Italic
End Using
RichTextBox1.Focus()
End Sub
Private Sub ChkBoxUnderline_Click(sender As Object, e As EventArgs) Handles ChkBoxUnderline.Click
Using f = RichTextBox1.SelectionFont
RichTextBox1.SelectionFont = New Font(f, f.Style Xor FontStyle.Underline)
ChkBoxUnderline.Checked = RichTextBox1.SelectionFont.Underline
End Using
ChkBoxItalic.Checked = RichTextBox1.SelectionFont.Italic
ChkBoxUnderline.Checked = RichTextBox1.SelectionFont.Underline
Panel1.BackColor = RichTextBox1.SelectionColor
End Sub
Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.SelectionColor = ColorDialog1.Color
Panel1.BackColor = ColorDialog1.Color
'RichTextBox1.SelectionStart = CaretPosn
'RichTextBox1.SelectionLength = 0
End If
RichTextBox1.Focus()
End Sub
Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged
ChkBoxBold.Checked = RichTextBox1.SelectionFont.Bold
ChkBoxItalic.Checked = RichTextBox1.SelectionFont.Italic
ChkBoxUnderline.Checked = RichTextBox1.SelectionFont.Underline
Panel1.BackColor = RichTextBox1.SelectionColor
End Sub
End Class
Public Class UndoRedoClass(Of T)
Private UndoStack As Stack(Of T)
Private RedoStack As Stack(Of T)
Public CurrentItem As T
Public Event UndoHappened As EventHandler(Of UndoRedoEventArgs)
Public Event RedoHappened As EventHandler(Of UndoRedoEventArgs)
Public Sub New()
UndoStack = New Stack(Of T)
RedoStack = New Stack(Of T)
End Sub
Public Sub Clear()
UndoStack.Clear()
RedoStack.Clear()
CurrentItem = Nothing
End Sub
Public Sub AddItem(ByVal item As T)
If CurrentItem IsNot Nothing Then UndoStack.Push(CurrentItem)
CurrentItem = item
RedoStack.Clear()
End Sub
Public Sub Undo()
RedoStack.Push(CurrentItem)
CurrentItem = UndoStack.Pop()
RaiseEvent UndoHappened(Me, New UndoRedoEventArgs(CurrentItem))
End Sub
Public Sub Redo()
UndoStack.Push(CurrentItem)
CurrentItem = RedoStack.Pop
RaiseEvent RedoHappened(Me, New UndoRedoEventArgs(CurrentItem))
End Sub
Public Function CanUndo() As Boolean
Return UndoStack.Count > 0
End Function
Public Function CanRedo() As Boolean
Return RedoStack.Count > 0
End Function
Public Function UndoItems() As List(Of T)
Return UndoStack.ToList
End Function
Public Function RedoItems() As List(Of T)
Return RedoStack.ToList
End Function
End Class
Public Class UndoRedoEventArgs
Inherits EventArgs
Private _CurrentItem As Object
Public ReadOnly Property CurrentItem() As Object
Get
Return _CurrentItem
End Get
End Property
Public Sub New(ByVal currentItem As Object)
_CurrentItem = currentItem
End Sub
End Class
Continue reading...