Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB^ question re Hex Colours

Status
Not open for further replies.

Cheiko

Technical User
Dec 8, 2004
2
GB
Hello

I am new to VB hence what is going to be a very simple question for those of you that are more familiar with it.

Can anyone tell me the Hex code for the following please?

I have a single label called Colour, with three scroll bars, Red, Blue, Green. My objective is as the scroll bars move the colour gradually changes in the label.

I can do this with the code

lblColour.BackColor = RGB(HSRed, HSGreen, HSBlue)

but need to know how to use the Hex Colour coding instead of HSRed, Green & Blue.

Many thanks


Cheiko
 
Just prefix your Hex values with &H.

Code:
HSRed = &HFF
HSGreen = &H0
HSBlue = &H0
lblColour.BackColor = RGB(HSRed, HSGreen, HSBlue)
'sets background to full red

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
I'm not sure what you're asking but you can assign colors as you are doing using the RGB function and you can also directly set a color by specifying a hex value. For example

lblColour.BackColor = &H80000005&

which is the windows background color.

Equivalently, the system defined ENUM "SystemColorConstants" has names for most of the system defined colors so the above could also be represented as

lblColour.BackColor = vbWindowBackground

Similarly, the ENUM "ColorConstants" has definitions for 8 common colors like vbRed, vbGreen, vbBlue, etc.

You can get into mixtures of different colors if you use HEX values directly like

lblColour.BackColor = &H00FF80FF&

and that is equivalent to

lblColour.BackColor = RGB (&H0F, &H08, &H0F)
 
If you want to do it without the RGB function, you could use the Hex function:

Dim BlueHex As String
Dim GreenHex As String
Dim RedHex As String
Dim ColorHex As String

Private Sub Blue_Scroll()
BlueHex = Hex(Blue.Value)
ColorHex = "&H" & RedHex & GreenHex & BlueHex
Label1.BackColor = ColorHex
DoEvents
End Sub

Private Sub Red_Scroll()
RedHex = Hex(Red.Value)
ColorHex = "&H" & RedHex & GreenHex & BlueHex
Label1.BackColor = ColorHex
DoEvents
End Sub

Private Sub Green_Scroll()
GreenHex = Hex(Green.Value)
ColorHex = "&H" & RedHex & GreenHex & BlueHex
Label1.BackColor = ColorHex
DoEvents
End Sub

Min of all scroll bars is 0, Max is 255.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Thank you for your replies, that's the thing with being a beginner - I'm not explaining myself very well.

I will try again...

I have 3 x horizontal scroll bars called Red, Blue & Green.

As I move the colour specific scroll bar the colour gradually changes in a label above (named Colour) e.g. when I move the Red scroll bar the colour changes through the 255 range from pink to deep red, as with the Blue & Green colour range.

I would like to know what I need to put as the colour specific horozontal scroll bar code instead of

lblColour.BackColor = RGB(HSRed, HSGreen, HSBlue)

I believe it is may be something like
lblColour.BackColor = RGB(255, 255, 255) - but this doesn't gradually change the colours.

There are so many ways to do the same thing - will I ever get the hang of this... !

Thank you all again.

 
The Hex$ function converts an Integer value to a string representing the equivalent Hex:

Hex$(255) will produce FF

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Cheiko,

If you put the following code in a form with the following it should work.

lblColour as a label
HSGreen, HSBlue, & HSRed as Horizontal Scroll bars with a Max of 255 and Min of 0

Code:
Private Sub Form_Load()
   ChangeColor
End Sub

Private Sub HSBlue_Change()
   ChangeColor
End Sub

Private Sub HSGreen_Change()
   ChangeColor
End Sub

Private Sub HSRed_Change()
   ChangeColor
End Sub

Private Sub ChangeColor()
   lblColour.BackColor = RGB(HSRed, HSGreen, HSBlue)
End Sub

-Sean
 
At this stage of your career, you may do better to look at some basic starter stuff. There are a number of beginners starting pages on the web, including:


and a whole page of others here:

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top