VB - Apend Text to a Richtextbox

By , 5/6/2011
(1 ratings)
5 times overloded Method to add a String to a Richtext box.

Syntax:
AppendText(Text,[Fcolor],[Bcolor],[bold])

Text = Text to add as String
Fcolor = Forground Color As System.Color
Bcolor = Background Color as System.Color
bold = set to boolean true for a bold font style


    ''' <summary>
    ''' Add a String to a RichTextbox
    ''' </summary>
    ''' <param name="Text"></param>
    ''' <remarks>AppendText(Text,[Fcolor],[Bcolor],[bold])  </remarks>
    Overloads Sub AddText(ByVal Text As String)
        AppendText(Text, Color.Black, Color.White, False)
    End Sub
    ''' <summary>
    ''' Add a String to a RichTextbox
    ''' </summary>
    ''' <param name="Text"></param>
    ''' <param name="BoolBold">set to true for bold font style</param>
    ''' <remarks>AppendText(Text,[Fcolor],[Bcolor],[bold] ) </remarks>
    Overloads Sub AddText(ByVal Text As String, ByVal BoolBold As Boolean)
        AppendText(Text, Color.Black, Color.White, BoolBold)
    End Sub
    ''' <summary>
    ''' Add a String to a RichTextbox
    ''' </summary>
    ''' <param name="Text"></param>
    ''' <param name="ForegroundColor">define a fore ground color</param>
    ''' <remarks>AppendText(Text,[Fcolor],[Bcolor],[bold])  </remarks>
    Overloads Sub AddText(ByVal Text As String, ByVal ForegroundColor As Color)
        AppendText(Text, ForegroundColor, Color.White, False)
    End Sub
    ''' <summary>
    ''' Add a String to a RichTextbox
    ''' </summary>
    ''' <param name="Text"></param>
    ''' <param name="ForegroundColor">define a fore ground color</param>
    ''' <param name="BoolBold">set to true for bold font style</param>
    ''' <remarks>AppendText(Text,[Fcolor],[Bcolor],[bold])  </remarks>
    Overloads Sub AddText(ByVal Text As String, ByVal ForegroundColor As Color, ByVal BoolBold As Boolean)
        AppendText(Text, ForegroundColor, Color.White, BoolBold)
    End Sub
    ''' <summary>
    ''' Add a String to a RichTextbox
    ''' </summary>
    ''' <param name="Text"></param>
    ''' <param name="ForegroundColor">define a fore ground color</param>
    ''' <param name="BackgroundColor">define a back ground color</param>
    ''' <remarks>AppendText(Text,[Fcolor],[Bcolor],[bold])  </remarks>
    Overloads Sub AddText(ByVal Text As String, ByVal ForegroundColor As Color, ByVal BackgroundColor As Color)
        AppendText(Text, ForegroundColor, BackgroundColor, False)
    End Sub
    ''' <summary>
    ''' Add a String to a RichTextbox
    ''' </summary>
    ''' <param name="Text"></param>
    ''' <param name="ForegroundColor">define a fore ground color</param>
    ''' <param name="BackgroundColor">define a back ground color</param>
    ''' <param name="BoolBold">set to true for bold font style</param>
    ''' <remarks>AppendText(Text,[Fcolor],[Bcolor],[bold])  </remarks>
    Overloads Sub AddText(ByVal Text As String, ByVal ForegroundColor As Color, ByVal BackgroundColor As Color, ByVal BoolBold As Boolean)
        AppendText(Text, ForegroundColor, BackgroundColor, BoolBold)
    End Sub
    Private Sub AppendText(ByVal text As String, ByVal ForegroundColor As Color, ByVal BackgroundColor As Color, ByVal BoolBold As Boolean)
        'text = text & vbNewLine
        Dim start As Integer = RichTextBox1.TextLength
        RichTextBox1.AppendText(text)
        Dim [end] As Integer = RichTextBox1.TextLength
        RichTextBox1.[Select](start, [end] - start)
        If True Then
            If BoolBold Then
                RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
            Else
                RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Regular)
            End If

            RichTextBox1.SelectionColor = ForegroundColor
            RichTextBox1.SelectionBackColor = BackgroundColor
        End If
        RichTextBox1.SelectionLength = 0
        RichTextBox1.ScrollToCaret()
        RichTextBox1.Refresh()
    End Sub
Tagged with Richtextbox, append string.

Comments

 

Log in, to comment!