|
-
Jul 4th, 2007, 06:13 PM
#1
Thread Starter
Lively Member
[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.
-
Jul 4th, 2007, 06:18 PM
#2
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
-
Jul 4th, 2007, 06:33 PM
#3
Frenzied Member
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
-
Jul 4th, 2007, 06:34 PM
#4
Thread Starter
Lively Member
Re: Label Forecolor Question
Thanks. Thats what I was after. :thumbsup:
-
Jul 4th, 2007, 08:39 PM
#5
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.
-
Jul 4th, 2007, 11:30 PM
#6
Thread Starter
Lively Member
Re: [RESOLVED] Label Forecolor Question
Thanks. Those are nice tidbits of info I will be storing away..
-
Jul 5th, 2007, 07:51 AM
#7
Re: [RESOLVED] Label Forecolor Question
 Originally Posted by Logophobic
@RhinoBull: That should be Int(256 * Rnd)...
Why should it be? I like it the way I posted.
-
Jul 5th, 2007, 10:21 AM
#8
Thread Starter
Lively Member
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.
-
Jul 5th, 2007, 05:10 PM
#9
Re: [RESOLVED] Label Forecolor Question
 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.
-
Jul 5th, 2007, 05:20 PM
#10
Re: [RESOLVED] Label Forecolor Question
 Originally Posted by Logophobic
Because Int((255 * Rnd) + 0) will give a result ranging from 0 to 254.
So what ?
-
Jul 5th, 2007, 05:31 PM
#11
Re: [RESOLVED] Label Forecolor Question
Don't you want a result ranging from 0 to 255?
-
Jul 5th, 2007, 05:41 PM
#12
Re: [RESOLVED] Label Forecolor Question
 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.
 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>
-
Jul 5th, 2007, 07:19 PM
#13
Re: [RESOLVED] Label Forecolor Question
 Originally Posted by Logophobic
Don't you want a result ranging from 0 to 255?
 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... It would work as if I would use 23 to 247 or something...
I really see no point. Common.
-
Jul 5th, 2007, 07:26 PM
#14
Thread Starter
Lively Member
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.
-
Jul 5th, 2007, 07:38 PM
#15
Re: [RESOLVED] Label Forecolor Question
Sorry Brian, I certainly noticed that but somebody else didn't...
Cheers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|