please help with my project

here it goes...


I want the every text in my label to be different color

example
label text is Visual

the color of V is black and the rest will be red and if I press the key i the letter i will be black...

Recommended Answers

All 2 Replies

Hi PutingPanday,

I do not believe the Label control has any properties or methods to change the color of individual characters. You can kind of hack the richtextbox by shrinking it to the size that your text needs, change the background color to the color of its container, and set its border to none then use something similar to the following

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    rtbColumnInfo.SelectionStart = rtbColumnInfo.Find("V")
    rtbColumnInfo.SelectionColor = Color.Blue
    rtbColumnInfo.SelectionStart = rtbColumnInfo.Find("i")
    rtbColumnInfo.SelectionColor = Color.FloralWhite

  End Sub

Just a thought ...Good Luck
Ken

As Phasma mentioned, a rtb(RichTextBox) would be the way to go; at least for a vb.noob.

See if this helps.
1.rtb, 1.btn(Button)

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True '// keep.Focus on Form while typing.
        RichTextBox1.ReadOnly = True '// disable typing.
    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        With RichTextBox1
            For i As Integer = 0 To .Text.Length - 1 '// loop thru text.
                .Select(i, 1) '// select one letter at a time.
                If Not .SelectionColor = Color.Black Then '// check selected letter's color.
                    If .SelectedText.ToLower = e.KeyChar.ToString.ToLower Then '// check if letter pressed is the next letter to color.change.
                        .SelectionColor = Color.Black '// change letter's color.
                    End If
                    Exit For '// exit loop since done and possibly thank .Me later. :D
                End If
            Next
        End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        addWord("Visual")
    End Sub

    Private Sub addWord(ByVal selWord As String)
        With RichTextBox1
            .Clear()
            .Text = selWord '// add word.
            .ForeColor = Color.Red '// set color.
            .Select(0, 1) '// select 1st letter.
            .SelectionColor = Color.Black '// change letter's color.
        End With
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.