Ulyzses Ulyzses - 4 months ago 17
C# Question

'Label' does not contain a definition for 'ForeColor'.

Basically, I have a button, that, when pressed, should change another label's text color. However, whenever I try to run it, I get the error:


'Label' does not contain a definition for 'ForeColor' and no extension method 'ForeColor' accepting a first argument 'Label' could be found (are you missing a using directive or an assembly reference?)


The basic code is:

private void Button_Click(object sender, RoutedEventArgs e)
{
tlabel.ForeColor = System.Drawing.Color.Red;
}


What should I do?

Sorry if the question might be easily fixable but I just recently started with C# and I couldn't find any solutions that fix my problem (or questions that are even similar).

Answer Source

The property is called Foreground and it's a Brush, not a Color.

tlabel.Foreground = System.Windows.Media.Brushes.Red;

The advantage of using a brush instead of a color is that red is just red, but a Brush could be a lot of things. The system brush I showed you is a SolidColorBrush -- just red -- but there are various gradient brushes, ImageBrushes, and so on.

System.Drawing is a windows forms namespace, not WPF. ForeColor is a windows forms property as well. Make sure the documentation you're looking at is for WPF, not windows forms.