setting backcolor using a string
Hmmm... I have a string , say
strUncleFred = "vbRed"
or
strUncleFred = "vbGreen"
then I want
textGoat.BackColor = strUncleFred
But I get a type mismatch error. I see that .backcolor expects a long value, so I tried using
Clng(strUncleFred) ....but that got me nowhere
I really want to use only the simple Vb colors specified when the use types in a string.
I can see that vbRed is a constant and "vbRed" is something else--a string, but can you convert one to the other so easy way?
Re: setting backcolor using a string
Don't use QUOTES. That makes it a STRING by default.
Code:
Private Sub Form_Load()
Dim vb As Long
vb = vbRed
Form1.BackColor = vb
End Sub
Re: setting backcolor using a string
It is ALREADY being entered as a string (actually as part of a longer string), I was just showing an example.
so if the string is "vbRed", etc how can you use it to set the color?
Re: setting backcolor using a string
Store colors as LONG, like I did. Try the sample. It prints 255, then changes the backcolor of the form to 255/vbRed
Re: setting backcolor using a string
David, sometimes even me does not understand what you are referring to.
What sample prints 255 and changes the backcolor of the form to 255/vbRed???
What the OP wanted, I think, was that a literal string like "vbRed" he has in a string variable, is to be converted to the according color value. This, I'm afraid is not possible ad hoc, since these constants are only known to the compiler, not to the runtime facility. Also the famous script control we had in discussion recently will not resolve these constants, I think.
You can, however, write your own conversion function like this:
Code:
Private Function LiteralToColorValue(ColorLit As String) As Long
Dim c As Long
Select Case LCase(ColorLit)
Case "vbred": c = vbRed
Case "vbgreen": c = vbGreen
Case "vbblue": c = vbBlue
'...
End Select
LiteralToColorValue = c
End Function
You would have to add a Case statement for every literal you want to have converted. There are some more, I suppose, like vbWhite, vbBlack, vbYellow and so on. This is the only choice I can see at the moment.
Re: setting backcolor using a string
Quote:
Originally Posted by
dglienna
Don't use QUOTES. That makes it a STRING by default. oops. edited out the msgbox statement
Code:
Private Sub Form_Load()
Dim vb As Long
vb = vbRed
MsgBox vb
Form1.BackColor = vb
End Sub
Turns the form RED as you step thru
Re: setting backcolor using a string
Ok. I see. And I didn't understand 255/vBred either. Sorry. Got it now. :)