assign color attribute to control from a variable

ninjaimp

Well-known member
Joined
Jun 13, 2007
Messages
80
Programming Experience
1-3
hi

I have a system color value stored in a variable e.g. (Color [A=255, R=255, G=128, B=128])

and i would like to assign that to the back color of a form but i keep getting the error:

" Control does not support transparent background colors. "

my code:

VB.NET:
       formcolor = Color [A=255, R=255, G=128, B=128]

        Dim testcolor As Color

        testcolor = Color.FromName(formcolor)

        Me.BackColor = testcolor

any help appreciated

regards
 
First up, that code you posted simply wouldn't compile. You'd have to have had double quotes around the "Color [A=255, R=255, G=128, B=128]" part. The problem is then that you're trying to use that as a name to create a Color value. There is no such named Color so you end up with (0,0,0,0) as the ARGB values and 0 is not a valid alpha value for a form.

Solitaire is quite correct but getting those values from that string is not trivial. It's far from impossible but it would be a pain. You'd be far better storing the entire Color value in a single Integer that can be used with FromArgb and ToArgb. If you want to be able to edit the value in serialised form than it may be better to store it as four separate integer values, or rather just three because the alpha value must be 255 always anyway.
 
still having problems

I have now set the variable as a string i.e

VB.NET:
formcolor = Color [A=255, R=255, G=128, B=12]

And tring to set the back as this...
VB.NET:
        testcolor = Color.FromArgb(CInt(formcolor))

        Me.BackColor = testcolor
but i get the error:

VB.NET:
Conversion from string "Color [A=255, R=255, G=128, B=12" to type 'Integer' is not valid.
 
You can use the shared ColorTranslator.ToWin32/FromWin32 methods to get/set an integer value for a Color object, or as easy as jmc said the Color.FromArgb/ToArgb methods.
The shared Color.FromName method will allow to parse the string you have to a Color object if you need that.
 
Does this:
Color [A=255, R=255, G=128, B=12]
look like an integer to you? How exactly would you expect that string to get converted to an Integer? Of course it can't. If you want to use that string then you're going to have to extract the numbers out of it first and then use those to create the Color. I was under the impression that I'd already said that but obviously it wasn't clear enough. Do you really have to use a string in that format? Can you not do what I suggested in my previous post?
 
still having problems

I have now set the variable as a string i.e

VB.NET:
formcolor = Color [A=255, R=255, G=128, B=12]

Why that string? Why not make it easy on yourself:

formcolor = "255,128,12"

Then you can split it with string.split and pul lthe intergers out:

VB.NET:
Dim bits() as String = formcolor.Split(",".ToCharArray())
Me.Backcolor = Color.FromArgb(255, Convert.ToInt32(bits(0)), Convert.ToInt32(bits(1)), Convert.ToInt32(bits(2)))

Actually if you insist on having all that extra crap in your string, this should work:
VB.NET:
Dim bits() as String = formcolor.Split("[],=ABCGloRr ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
Me.Backcolor = Color.FromArgb(255, Convert.ToInt32(bits(1)), Convert.ToInt32(bits(2)), Convert.ToInt32(bits(1)))
 
i know it doesnt look like an integer and in the first instance didnt change it, but i have just seen where my problem was - i hadnt converted the color value to integer when storing it, it is now working fine using Solitaire method!

many thanks for all your help
 
Why that string?
That is the string format Color structure serialize to and from for custom color values. There are some options here depending on the purpose and use.
 
Back
Top