[RESOLVED] Label Forecolor Question
What kind of value would I use in a situation like this. I would like to make the color random.
vb Code:
label1.forColor = 'What value can I use to change the forColor of the label?
'I will be assigning it a random value here. I need to know
'what kind of variables and such this will accept? Thanks.
Re: Label Forecolor Question
You can use the RGB funcrion:
Code:
Option Explicit
Private Sub Command1_Click()
Dim r, g, b
r = Int((255 * Rnd) + 0)
g = Int((255 * Rnd) + 0)
b = Int((255 * Rnd) + 0)
Label1.ForeColor = RGB(r, g, b)
End Sub
Private Sub Form_Load()
Randomize
End Sub
Re: Label Forecolor Question
For just 16 colours you can use the code:
Code:
Private Sub Form_Load()
Randomize
Label1.ForeColour = QBColor(Int(Rnd * 16))
End Sub
Re: Label Forecolor Question
Thanks. Thats what I was after. :thumbsup:
Re: [RESOLVED] Label Forecolor Question
@RhinoBull: That should be Int(256 * Rnd)
Another option for a random 24-bit color:
Code:
Label1.ForeColor = Int(Rnd * &H1000000) ' &H1000000 = 16777216 = 2^24
This gives the same range of results as the RGB function.
Re: [RESOLVED] Label Forecolor Question
Thanks. Those are nice tidbits of info I will be storing away..
Re: [RESOLVED] Label Forecolor Question
Quote:
Originally Posted by Logophobic
@RhinoBull: That should be Int(256 * Rnd)...
Why should it be? I like it the way I posted.
Re: [RESOLVED] Label Forecolor Question
Rhinobull's example worked. I ended up changing it to Int((205 * Rnd) + 50) to get a random number between 50 and 255. It worked also. The range of rgb extends from 0 to 255, at least on other compilers I have used. So I think that a 256 would give a subscript out of range error or something. Thats the great thing about programming, so many different ways to solve the same problem.
Re: [RESOLVED] Label Forecolor Question
Quote:
Originally Posted by RhinoBull
Why should it be? I like it the way I posted.
Because Int((255 * Rnd) + 0) will give a result ranging from 0 to 254.
For a random number from a given range, use Int((High - Low + 1) * Rnd) + Low.
Re: [RESOLVED] Label Forecolor Question
Quote:
Originally Posted by Logophobic
Because Int((255 * Rnd) + 0) will give a result ranging from 0 to 254.
So what ?
Re: [RESOLVED] Label Forecolor Question
Don't you want a result ranging from 0 to 255?
Re: [RESOLVED] Label Forecolor Question
Quote:
Originally Posted by RhinoBull
So what ?
Ooh, RhinoBull, swallow your pride. Your post implies to readers that RND might sometimes return a 1, which is wrong.
Quote:
Originally Posted by Brian M.
<snip>The range of rgb extends from 0 to 255, at least on other compilers I have used. So I think that a 256 would give a subscript out of range error or something.<snip>
Re: [RESOLVED] Label Forecolor Question
Quote:
Originally Posted by Logophobic
Don't you want a result ranging from 0 to 255?
Quote:
Originally Posted by Milk
Ooh, RhinoBull, swallow your pride...
Both of you seemed to be missing a point - this thread isn't about getting unique random number withing specific range so it's not about Rnd as well
It's all about generating different colors. But you you're arguing 1 (one) darn color... :confused: It would work as if I would use 23 to 247 or something...
I really see no point. Common. :rolleyes:
Re: [RESOLVED] Label Forecolor Question
Hey, I was fine with the results. I appreciate the time you took to answer my query. If you notice this thread is resolved.
Re: [RESOLVED] Label Forecolor Question
Sorry Brian, I certainly noticed that but somebody else didn't... :)
Cheers.