An object-oriented programming language developed by Microsoft that can be used in .NET.
Use a Windows Forms ColorDialog, then assign its selected Color to the control’s ForeColor property when the user clicks OK.^1^
Let the user choose the font color at runtime
- Open the form in the Visual Studio Designer.
- From Toolbox → Dialogs, drag a ColorDialog onto the form. It appears in the component tray as
ColorDialog1. - Add a button named
ButtonChooseColor. - Double-click the button and add:
Private Sub ButtonChooseColor_Click(
sender As Object,
e As EventArgs
) Handles ButtonChooseColor.Click
ColorDialog1.Color = Label1.ForeColor
If ColorDialog1.ShowDialog() = DialogResult.OK Then
Label1.ForeColor = ColorDialog1.Color
End If
End Sub
Replace Label1 with the control whose text should change, such as:
TextBox1.ForeColor = ColorDialog1.Color
Button1.ForeColor = ColorDialog1.Color
RichTextBox1.ForeColor = ColorDialog1.Color
Setting ColorDialog1.Color before showing the dialog makes the control’s current text color the initially selected color. If no color has been selected, the dialog’s default Color value is black.^2^
To apply the selected font color to the form itself and controls that inherit its foreground color, use:
If ColorDialog1.ShowDialog() = DialogResult.OK Then
Me.ForeColor = ColorDialog1.Color
End If
Set a fixed color instead
If the color does not need to be selected at runtime, set it in either of these ways:
- Designer: Select the control → Properties → ForeColor → choose a color.
- Code:
Private Sub Form1_Load(
sender As Object,
e As EventArgs
) Handles MyBase.Load
Label1.ForeColor = Color.Crimson
End Sub
ForeColor changes the text color; BackColor changes the background color.
Keep the choice after restarting the application
If the selected color must remain after the application closes, store it in a user-scoped application setting. Only user-scoped settings can be changed and persisted at runtime; application-scoped settings are read-only.^3^
Create a setting such as FontColor with:
- Type:
System.Drawing.Color - Scope:
User - Default value:
Black
Then save and restore it:
' After the user selects a color:
Label1.ForeColor = ColorDialog1.Color
My.Settings.FontColor = ColorDialog1.Color
My.Settings.Save()
' In the form's Load event:
Label1.ForeColor = My.Settings.FontColor
References