After much toiling and testing I've finally come up with a solution that works.
Initially I was looking for a way of simply setting all of the Properties of a class automatically from a passed Object. I would them pass that set of properties, together with a String
, to a function that would update a RichTextBox
, using the properties to format the text that was to be appended.
However, it became clear that this would not work because many of the Properties of a RichTextBox
are read-only
(such as Bold
).
So I decided to pass my desired settings and update the RichTextBox
all in one, with all of the work now being done by the ProgressUpdate
Class.
Thanks to @Neolisk and @AaronPalmer for talking things through with me. I reaslise that this question was not 100% clear/accurate, yet you tried to help any way, for which I am grateful.
Here is an example of how I will use the ProgressUpdate
Class -
Class aForm
Private Property ProgressUpdate As ProgressUpdate
Public Sub New()
Me.ProgressUpdate = New ProgressUpdate(Me.CurrentProgressTextbox)
End Sub
Private Sub SettingsButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SettingsButton.Click
Call Me.ProgressUpdate.Update("Clicked Settings...")
End Sub
Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CancelButton.Click
Call Me.ProgressUpdate.Update({{"Text", "You cancelled!!!"}, {"FontStyle", FontStyle.Bold}, {"FontSize", 12.0}})
End Sub
End Class
And here is the Class itself -
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Public Class ProgressUpdate
Private Property TextBox As RichTextBox
Private Property Args As Dictionary(Of String, Object)
Private Property Font As Font
'**
' Constructor
'*
Public Sub New(ByVal TextBox As RichTextBox)
Me.Textbox = TextBox
End Sub
'**
' Update 'TextBox' with the desired text and style
'
' @param RawArgs Object The Args to use when updating the Textbox (can be either String or Array)
' @param Append Boolean Whether or not the Append the Text (Text is overwritten if False)
'*
Public Sub Update(Optional ByVal RawArgs As Object = Nothing,
Optional ByVal Append As Boolean = True)
'** Set up Args and ensure that they are valid *'
Call Me.SetArgs(RawArgs)
If Me.Args Is Nothing Then Exit Sub
'** Make sure that Text is declared and set the Text property *'
Dim Value As Object = Nothing
If Not Me.Args.TryGetValue("Text", Value) Then Exit Sub
Dim Text As String = CType(Value, String)
Call Me.SetFont() ' Set up the Font to use for the text that is being added
With Me.TextBox
If Append Then ' Text is being appended
If .Text <> "" Then Text = Environment.NewLine & Text ' Add a new line before the Text if necessary
.AppendText(Text)
Else ' Text is being overwirtten
.Text = Text
End If
.Select(.GetFirstCharIndexOfCurrentLine(), Text.Length) ' Select all of the last line
.SelectionFont = Me.Font ' Set the desired font
.Select(.GetFirstCharIndexOfCurrentLine(), 0) ' Select the current line, with a length of 0
.ScrollToCaret() ' Scroll to the caret (to show the bottom line)
End With
End Sub
'**
' Set the Args property
'
' @param required RawArgs Object The Args to use when updating the Textbox (can be either String or Array)
'*
Private Sub SetArgs(ByVal RawArgs As Object)
If TypeOf RawArgs Is String Then ' e.UserState is a String, so it's just the Text that should be added to Args
Me.Args = New Dictionary(Of String, Object) From {{"Text", RawArgs}}
ElseIf TypeOf RawArgs Is Array Then ' e.UserState is an Array, so add all of the key/value pairs to Args
Me.Args = New Dictionary(Of String, Object)
For KeyIndex As Integer = 0 To UBound(RawArgs)
Me.Args.Add(RawArgs(KeyIndex, 0), RawArgs(KeyIndex, 1))
Next
Else : Me.Args = Nothing ' There were no Args set
End If
End Sub
'**
' Set the correct 'Font' for use on the line that is currently being added to the `TextBox`
'*
Private Sub SetFont()
Dim FontFamily As FontFamily = Nothing
Dim FontSize As Single = 0
Dim FontStyle As FontStyle = Nothing
Dim Value As Object
With Me.TextBox
Value = Nothing
If Me.Args.TryGetValue("FontFamily", Value) Then
If TypeOf Value Is FontFamily Then
FontFamily = Value
Else : FontFamily = .Font.FontFamily
End If
Else : FontFamily = .Font.FontFamily
End If
Value = Nothing
If Me.Args.TryGetValue("FontSize", Value) Then
If IsNumeric(Value) Then
FontSize = Value
Else : FontSize = .Font.Size
End If
Else : FontSize = .Font.Size
End If
Value = Nothing
If Me.Args.TryGetValue("FontStyle", Value) Then
If TypeOf Value Is FontStyle Then
FontStyle = Value
Else : FontStyle = .Font.Style
End If
Else : FontStyle = .Font.Style
End If
End With
Me.Font = New Font(FontFamily, FontSize, FontStyle)
End Sub
End Class