Convert int to color in Windows Forms

I have a value in the string equal to -16777056, but how do I pass it in color?

Something like that:

Color = (Color) Convert.ToInt32(((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"]) }) 
+4
source share
4 answers

Try this instead:

 var argb = Convert.ToInt32( ((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"]); Color = Color.FromArgb(argb); 
+9
source

The conversion between colors and numbers is called color-graphemic synesthesia , and a person with this condition can identify the color (or even the form or "feeling") of a number. However, one difficult point is that it has never been identified on a computer (in humans only). In addition, this condition is usually associated with genetics in humans, but has been reported after the use of a psychedelic drug. And although I would never suggest using illegal drugs, I think that you lose your processor, some LSD might be worth it.

Another difficulty is that the sinetet have no commonality between them, that is, the number 6 is not the same as for you. Thus, they may look different on your computer than on mine. (It’s kind of like β€œwow, man, how can I find out that the colorβ€œ blue ”is not like what I think isβ€œ red ”for you?” But then again, we went back to illegal drugs.)

The way I try to convert a number to color. That is, if your number does not actually represent something useful, for example, an ARGB color value. In this case you can use:

 Color.FromArgb(numericValue); 
+27
source

Well, you know how color was turned into this whole in the first place? I assume this is an ARGB value consisting of concatenated bytes.

If so, this is the easiest way:

 var myColor = Color.FromArgb(myColorInt); 

which will be integrated into your actual line of code, for example:

 var myColor = Color.FromArgb(Convert.ToInt32(((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"]) })); 
+6
source
 Color myColor = Color.FromArgb(Convert.ToInt32(((DataRowView) this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"])}); 

Edit: After reading one of your comments above, you can simplify things a bit. Try to assign the selected color as follows:

 ((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"] = colorDialog.Color; 

And then read it like this:

 Color myColor = ((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"]; 

This way you save it as Color and extract it as Color and skip the intermediate conversion steps.

This may or may not work with some other modifications, depending on whether your data source saves Color instead of int , as currently defined.

+1
source

All Articles