'How to change label color in vb.net using codes?

I want to know how to change the color of a label using its code, something like:

dim r as color
r = color.red

How do I use or assign that code to a label from a button...? Like in my form button and a label.. when the button is clicked the label changes its color .. how do I do that in vb.net?.

Here is my button

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


        Dim r As Color
        r = Color.Red

    End Sub

End Class

??



Solution 1:[1]

You have defined a variable which is not the right way to do it. A label exposes some properties and to achieve your goal you simply do this:

 Label1.BackColor = Color.Aqua

Solution 2:[2]

Use BackColor property

label1.BackColor=Color.Red

Solution 3:[3]

First add the following import statement to the top of your class

    Imports System.Drawing

Then use this code

   label1.BackColor=Color.Red

Solution 4:[4]

No need to set dim you can just use forecolor property.

in button_1 write this code:

   Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.ForeColor = Color.Aqua
    End Sub 
End class

Label1.ForeColor = Color.Aqua Here color.color_name you can use any color you want

now when you click on button_1 and the label.1 text color get changed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt Wilko
Solution 2 King of kings
Solution 3
Solution 4