By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

how do I use a variable to change a color

P: 1
I am creating a program with a changeable background color for a lot of buttons and I need to be able to save all of those colors and then open them back up when the program is opened again.

If I recall a textfile with the value "Blue" for color, then how can I use that?

I was thinking something like

------------------
abcde = "Blue"

Button1.Backcolor = Color.abcde
-------------------

But I don't know how to make that work
Sep 6 '07 #1
Share this Question
Share on Google+
7 Replies


kadghar
Expert 100+
P: 1,295
I am creating a program with a changeable background color for a lot of buttons and I need to be able to save all of those colors and then open them back up when the program is opened again.

If I recall a textfile with the value "Blue" for color, then how can I use that?

I was thinking something like

------------------
abcde = "Blue"

Button1.Backcolor = Color.abcde
-------------------

But I don't know how to make that work
the idea is good, but instead of saving thw word blue, why dont you save the RGB combination in a Color Array

dim CArr() as byte
'say you have 5 differente colours
redim carr(1 to 5, 1 to 3)
'you have Blue, Yellow, Black, White and Red
'For Blue
carr(1,1)=0
carr(1,2)=0
carr(1,3)=255

'and then when you load them just:
button1.backcolor=rgb(carr(1,1),carr(1,2),carr(1,3 ))
'and so on.

To know the combination of any color, just go to paint, there you have the palette

you an also use vb constants, i dont remember them but seems that
button1.backcolor=vbblue will do

HTH
Sep 6 '07 #2

VBPhilly
P: 95
When persisting such settings, I use the registry functions:
- SaveSetting
- GetSetting
Just a thought.
Sep 6 '07 #3

Robbie
100+
P: 180
I am creating a program with a changeable background color for a lot of buttons and I need to be able to save all of those colors and then open them back up when the program is opened again.

If I recall a textfile with the value "Blue" for color, then how can I use that?

I was thinking something like

------------------
abcde = "Blue"

Button1.Backcolor = Color.abcde
-------------------

But I don't know how to make that work
You seem to be asking how to actually refer to colours, since you know that "Blue" doesn't work.

You seem to already be able to save and load the word.

But you shouldn't save "Blue".
Save the BackColor property. For example, for Blue, you can get the number which represents that colour using RGB(0,0,255)
.
(RGB means Red, Green, Blue - each ranges from 0 to 255. So that example above represents no red, no green, maximum blue, = a pure blue colour).

RGB there will give you back 16711680. This is just a number that represents that colour. So save that number to the file, then when you reload it, set the appropriate object's BackColor value to cStr(ThatNumber).

I may have got the wrong end of the stick... if you actually want to know how to save and load from the text file, I can tell you that too, just ask. ;)

EDIT: In other words, colour values are simply numbers of Long type.
If you want to know more on how to decode these long numbers back to the actual Red, Green and Blue values, I explained roughly how to do so here:
http://www.thescripts.com/forum/thread701351.html
Sep 7 '07 #4

Expert 5K+
P: 8,434
Keep in mind, there's no reason why you can't use a more easily human-readable value such as "Blue". But you will need to write some code to convert between that and the "Real" colour value.
Sep 8 '07 #5

Robbie
100+
P: 180
Keep in mind, there's no reason why you can't use a more easily human-readable value such as "Blue". But you will need to write some code to convert between that and the "Real" colour value.

Oh yes, that's right, Sorry Philly...

You could always save "Blue", then when reading it, use a Select-Case statement or something to convert it back to RGB(255,0,0).

But, I was trying to give a more universal way of doing things, so you could give your users a whole gradient colour pallette, and they could pick any colour, without you having to hard-code the alternative 'words' for 16,581,375 different colour values. ^^;;
Sep 8 '07 #6

Expert 5K+
P: 8,434
Oh yes, that's right, Sorry Philly...
I don't think there's any need to apologise. Storing the actual value is almost certainly the preferred method. I just wanted to point out that you always have the option of doing these things pretty much any way you want, it may just require more work.

I'm actually in favour of making data files human-readable wherever possible, just so you can tell what they mean when you see them in a couple of years, after forgetting the program.
Sep 9 '07 #7

Ali Rizwan
100+
P: 927
OK
If you want to apply colors fetching color names from text file. If yes then following file will help u a lot.
Attached Files
File Type: zip Colors.zip (1.6 KB, 222 views)
Sep 9 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.