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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Backcolour of Label to a hexnumber

Status
Not open for further replies.

BiggerD

Programmer
Jun 19, 2002
43
GB
I am using VB6 and am trying to set the back colour or a label to a hexidecimal number that a user enteres into a textbox, (so they can preview their colour).

using the syntax label.backcolour = rgb(123,123,123) all works fine as you would expect.

However trying to set the backcolour to a hex value from a text box fails due to the value being a string.

Am i being think, how do i go about doing this?
Currently have tried
label.backcolor = txthex.text
label.backcolor = val(txthex.text)
label.backColour = clng(txthex.text)
label.backColour = cint(txthex.text)

anyone got any ideas?

 
Try...

label.backColour = clng([!]"&h" & [/!]txthex.text)

This assumes that the user will be typing values like:

000000
FFFFFF

You'll want to make sure that ONLY 0-9 and A through F are entered in to the text box or you will get a type mismatch.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
<You'll want to make sure that ONLY 0-9 and A through F are entered in to the text box or you will get a type mismatch.

In order to do this, investigate the KeyPress event.

Bob
 
May be my question was phrased wrongly, I know how to set the colour my question should have been how to take a 'hex value' from the text property of a text box, which is a datatype of string, convert it to hex then set the back colour.
I eventaully found this code that converts a string to a hex number and the rest is easy.

Private Function Hex2Dec(ByVal HexString As String) As Variant
Dim X As Integer
Dim BinStr As String
If Left$(HexString, 2) Like "&[hH]" Then
HexString = Mid$(HexString, 3)
End If
If Len(HexString) <= 23 Then
Const BinValues = "0000000100100011" & _
"0100010101100111" & _
"1000100110101011" & _
"1100110111101111"
For X = 1 To Len(HexString)
BinStr = BinStr & Mid$(BinValues, _
4 * Val("&h" & Mid$(HexString, X, 1)) + 1, 4)
Next
Hex2Dec = CDec(0)
For X = 0 To Len(BinStr) - 1
Hex2Dec = Hex2Dec + Val(Mid(BinStr, _
Len(BinStr) - X, 1)) * 2 ^ X
Next
Else
' Number is too big, handle error here
End If
End Function
 
What is wrong with gmmastros' solution?

The Hex2Dec function you have found is really designed to deal with those situations in which you are presented a hex string longer VB can handle natively. But VB can certainly natively handle any hex string that represents a color, so you really don't need Hex2Dec and gmmastros' solution achieves much the same result
 
I'm glad you found a solution, but now you have me curious. Can you please post some examples that would be in the text box (i.e. passed in to this function)?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
stronm,
I had tried the suggestion from gmmastros and found i kept getting a type conversion error i.e. string to long.

gmmastros,
i am allowing the user to key in the hex value if they have it to choose exact colour, for example to exatly match a corporate back colour of their marketing liturature.

Many thanks for you help.
 
Well, if the string they are puttinig in is too long for gmmastros' solution (8 hex characters) then it doesn't represent a legitimate color value ...
 
it will not be too long and will only contain value hex values i.e. 0 - 9 a - f
 
In which case there is no reason I can see why the gmmastros solution is not working for you
 
BiggerD,

I was playing around with this a little, and would like to change my suggestion (to make it a little 'safer').

I created a new VB Project, added 2 labels (Label1 and Label2) a text box, and a comand button. I added your function to the form and this code...

Code:
Option Explicit

Private Sub Command1_Click()
    
    Dim sHex As String
    
    Label1.BackColor = Hex2Dec(Text1.Text)
    
    sHex = Text1.Text
    
    If sHex Like "&[Hh]" Then
        sHex = Right(sHex, Len(sHex) - 2)
    End If
    
    If sHex Like "*[0-9A-F]*" Then
        sHex = Right(sHex, 6)
        Label2.BackColor = CLng("&h" & sHex)
    End If
        
End Sub

I noticed that the back color of the labels appear identical. As StrongM suggests, if the number is larger than a long, it's an invalid color. My code snippet above takes the 6 right most characters of the text box and uses it as the color (similarly to your code).


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Given that we have already established that the text string only contains 0-9 and A-F you wouldn't expect to find '&' or 'h' or 'H' in it ...

And it has also been stated that the string will not be too long ...

I better also point out that there are only a very limited set of legitimate color values greater than 16777215
 
Someone should point out that this is a user-hostile interface. How about using some sliders (or other visual controls) for the user to manipulate the color? You could display the hex values if you wanted, but if the OP's idea is "so they can preview their colour," ease-of-use would better be served almost any way other than entering hex in text.
 
Oh well, in that case you'd be best off using the OLE color picker ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top