.NET Color Struct Equality

When is white not white? When one is Color.White and the other is Color.FromArgb(0xff, 0xff, 0xff, 0xff).

I was trying to data bind a list of colors to a ComboBox and have the SelectedValue property bound to a particular color. The frustrating thing was that even though there was a color matching the “selected” color in the list, the ComboBox never had a selection when first displayed.

Eventually I discovered that my “selected” color was white (all color components at 255) while the list’s equivalent was Color.White. Careful reading of the Color.Equals documentation tells me that I was silly to think that white is white or black is black:

Black and FromArgb(0,0,0) are not considered equal, since Black is a named color and FromArgb(0,0,0) is not.

Leave a Reply

Your email address will not be published. Required fields are marked *