Results 1 to 15 of 15

Thread: [RESOLVED] Label Forecolor Question

  1. #1

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Resolved [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:
    1. label1.forColor = 'What value can I use to change the forColor of the label?
    2.                        'I will be assigning it a random value here. I need to know
    3.                        'what kind of variables and such this will accept? Thanks.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  3. #3
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    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

  4. #4

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Re: Label Forecolor Question

    Thanks. Thats what I was after. :thumbsup:

  5. #5
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    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.

  6. #6

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    Re: [RESOLVED] Label Forecolor Question

    Thanks. Those are nice tidbits of info I will be storing away..

  7. #7

  8. #8

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    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.

  9. #9
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    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.

  10. #10

  11. #11
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] Label Forecolor Question

    Don't you want a result ranging from 0 to 255?

  12. #12
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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>

  13. #13
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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... It would work as if I would use 23 to 247 or something...
    I really see no point. Common.

  14. #14

    Thread Starter
    Lively Member Brian M.'s Avatar
    Join Date
    Nov 2006
    Location
    Moberly MO
    Posts
    94

    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.

  15. #15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width