-1

How do you get the color of a control, such as a label or rectangle? I need to do something like this:

if (label.foreground == #FFFFFF)
    Messagebox.Show("Branco!").

For awhile, I resolved my case with this:

SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromRgb(255, 255, 255);
if (rec.Fill == mySolidColorBrush)
    MessageBox.Show("Branco!");

Here are the errors I'm getting when I try the answers already posted:

enter image description here


enter image description here


enter image description here

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Noberto Pessôa
  • 125
  • 3
  • 12

3 Answers3

0

Now that I know you're using WPF, here's how to set the color on your label. Use the Foreground property and set it to a valid Brushes value.

if (label.Foreground = Brushes.White)
    MessageBox.Show("Branco!");

Same with the rectangle:

var rect = new Rectangle();
rect.Fill = Brushes.Green;
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0
if (label.ForeColor == System.Drawing.Color.Black) 
{
    // Do something here
}
matth
  • 6,112
  • 4
  • 37
  • 43
0

according to MSDN the Type of ForeColor is System.Drawing.Color, which mean you need to compare your element color with a predefined color in System.Drawing.SystemColors

EDIT

kindly check these question
Changing background color of the form with hexadecimal code
convert hex code to color name

try something like this

if (label.ForeColor == Color.FromArgb(0xFFFFFF)) Messagebox.Show("Branco!");
Community
  • 1
  • 1
ebram khalil
  • 8,252
  • 7
  • 42
  • 60