A Multi Color Label (actually a RichTextBox) with vb.net

Good Morning! I watched my daughter triumph in her first volleyball game of the year. And she is really GOOD! Actually the whole team had their fundamentals down pretty good. They are going to be quite good if they play together a lot.

Today’s topic comes courtesy of a reader who requested help to make a label …. multi colored. They want to be able to change the fonts and colors of words within the label. I didn’t really get into exactly why because I didn’t;t really want to know.  Coding like this usually turns me off because it serves no useful purpose. So instead of using a label, I recommended a rich text box using the code below. Make it a great day!


Join me on Facebook

”’ <summary>
    ”’ sets a form’s title to a standard – Dual Color title
    ”’
    ”’ for each form that you want to have a standard title on, you only need to:
    ”’ a) drop a RichTextBox on your form, with a meaningful name and suitable properties [Appearance(Font)] / [Layout(Anchor),(Dock),(Size)], and
    ”’ b) call this subroutine which uses armor plate text and colors found in the application’s settings.
    ”’
    ”’ To use, place this subroutine in a standard module to which you have access from a given form’s code
    ”’ 1. add an Application setting called ‘FormTitle’, type of ‘String’, value of whatever you like
    ”’ 2. add an Application setting called ‘FormTitleForeColor’, type of ‘System.Drawing.Color’, value of whatever color you like
    ”’ 3. add an Application setting called ‘FormTitleSubstring’, type of ‘String’, value of the substring in the title to have a new color
    ”’ 4. add an Application setting called ‘FormTitleSubstringForeColor’, type of ‘System.Drawing.Color’, value of substring’s color
    ”’ 5. Add a RichText Box control to your form. 
    ”’ 6. give it a meaningful name for use in your code, such as: rtbSample
    ”’ 6. set rtbSample properties as you normally would., i.e. some text, sizing, etc.  don’t forget to perhaps dock it.
    ”’ 7. in the form’s load procedure:
    ”’ InitializeFormTitle(Me.rtbSample)
    ”’ </summary>
    ”’ <param name="pRtb"></param>
    ”’ <remarks></remarks>

    Public Sub InitializeFormTitle(ByVal pRtb As RichTextBox)
        If (My.Settings.PropertyValues("FormTitle") IsNot Nothing) AndAlso (My.Settings.PropertyValues("FormTitleForeColor") IsNot Nothing) Then
            pRtb.Text = My.Settings.FormTitle.Trim
            pRtb.ReadOnly = True
            pRtb.Enabled = False
            pRtb.BorderStyle = BorderStyle.None
            pRtb.SelectAll()
            pRtb.SelectionAlignment = HorizontalAlignment.Center
            pRtb.SelectionColor = My.Settings.FormTitleForeColor
            pRtb.SelectionFont = New Font(pRtb.SelectionFont, FontStyle.Bold)
            If (My.Settings.PropertyValues("FormTitleSubstring") IsNot Nothing) AndAlso (My.Settings.PropertyValues("FormTitleSubstringForeColor") IsNot Nothing) Then
                If My.Settings.FormTitleSubstring.Trim.Length > 0 Then
                    Dim m_x As Integer
                    m_x = My.Settings.FormTitle.IndexOf(My.Settings.FormTitleSubstring.Trim)
                    If m_x > -1 Then
                        pRtb.Select(m_x, My.Settings.FormTitleSubstring.Trim.Length)
                        pRtb.SelectionColor = My.Settings.FormTitleSubstringForeColor
                        pRtb.SelectionFont = New Font(pRtb.SelectionFont, FontStyle.Bold)
                    End If
                End If
            End If
            pRtb.DeselectAll()
        End If
    End Sub


Join me on Facebook

Technorati Tags: ,,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: