Results 1 to 14 of 14
  1. #1
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Display Color of Variables

    Hello, is there any way to set the display colour of variables when using vba? I have a label on a user form that is made up of some text and a variable. I'd like the variable to stand out in some way, for example, have a different colour or be bolder than the rest of the text. Is this in any way possible?

    Thanks

    Chris (Hunt)

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Display Color of Variables

    You mention VBA but you have posted this in the Web Design forum. What exactly are you asking about?

  3. #3
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display Color of Variables

    Sorry Hans,

    I posted this to the wrong forum. I didn't notice the vba forum when I was posting. My question is about setting the colour of a variable used in a vba user form label. Is it possible to move the post?

    Regards,

    Chris

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Display Color of Variables

    <P ID="edit" class=small>(Edited by jscher2000 on 24-Nov-08 22:39. )</P>Moved!

    Do you have the property inspector displayed? It should allow you to manipulate foreground and background colors for most controls.

    Added: Here's a screen shot from Office 2007, but 2003 should be the same.
    Attached Images Attached Images

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Display Color of Variables

    The caption of a label and the text of a textbox control use a single font - you cannot format part of the caption or text differently from the rest.
    The easiest workaround is to use two controls; you can format them independently of each other.

    (There is a richtext control, but it's a PITA to work with and chances are that use of it is disabled for security reasons)

  6. #6
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display Color of Variables

    I think I understand your problem. You want your label to look something like this?
    Here is the static part of the label and here is the <font color=red>variable</font color=red> part

    To do this you'll need to butt several labels together:
    [Label1][<font color=red>Label2</font color=red>][Label3]

    To get the alignments looking acceptable, without excessive white space, I'd suggest choosing a wording where <font color=red>variable</font color=red> is at the extreme right, unless "variable" is always of the same consistently known length.

    Alan

  7. #7
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display Color of Variables

    Thanks Jefferson, yes I have the property inspector displayed. Alan has hit on a solution later down the thread.

    Best wishes,

    Chris

  8. #8
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display Color of Variables

    Thanks Paul, that's very interesting. I'll keep that for study purposes.

    Best wishes,

    Chris

  9. #9
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display Color of Variables

    Thanks Alan. Yes, that is what I am after. I was wondering about the solution you presented. Aligning the labels might be a little tricky but after reading your post I think it is doable. What the label does is ask the user about the sound of a combination of letters eg

    Word: choose the sound of ch in CHat
    Word choose the sound of ch in sCHool

    So actually there are two variables the first one, the letter combination being considered, and the second one, the word it is part of. Both variables vary in length. But the length of the first variable is known so I guess I can adjust the position of the last two labels by using object.left (I'll need four labels all together).I guess I'll see how the final form looks in action before deciding what to do. For letter combinations like "ch" there is no real need to colour the variable but for "a" there is possibly some confusion. Perhaps putting the variable in quotes would be sufficient.

    Best wishes,

    Chris

  10. #10
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Hatsukaichi, Hiroshima, Japan
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display Color of Variables

    Thanks Hans, I was hoping there might be a way which is why I posted, but my hope was very small <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Best wishes,

    Chris

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Display Color of Variables

    Yet another option: you could put two labels on top of each other, with the same size. Set the font for both to a fixed-width font such as Courier New.
    Set the BackStyle property of the top one to transparent, and set its font color to red (or at least something different from the bottom one).
    Set the Caption of the bottom label to the text you want to display, and that of the top one to just the characters you want to highlight, filled out with spaces, e.g.
    <pre> Me.Label1.Caption = "Choose the sound of ch in sCHool"
    Me.Label2.Caption = " CH "
    </pre>

    See the attached very simple demo document (you have to enable macros).
    Attached Files Attached Files

  12. #12
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Display Color of Variables

    <P ID="edit" class=small>(Edited by AlanMiller on 26-Nov-08 11:04. )</P>Looking a little deeper, I hadn't realized that Labels have a .Autosize property. By setting all of these to True, you could make a "chain" of labels butt up together using appropriate .Left and .Width properties. Finally, set their .Visible properties to True and that should give the result you're after. e.g.


    <table border=1><td>The sound of*</td><td><font color=red>ch</font color=red></td><td>*in S</td><td><font color=red>ch</font color=red></td><td>ool</td></table>Alan
    Edit: Setting the label .BorderStyle to fmBorderStyleNone (=0) of course.

  13. #13
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Display Color of Variables

    Hi Chris,

    Here's a vba userform that uses a spinner to cycle a colour display through the visible spectrum (360-800nm), starting off with the RGB equivalent of a wavelength of 555nm (the one the human eye is most sensitive to). I developed the code to approximate the known RGB values for a photography app I was working on. The spinner increments/decrements in 5nm steps (but you can use larger or smaller steps). The included code should give you plenty of RGB variables to work with, without having to resort to composite colours (eg brown).
    Attached Files Attached Files
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  14. #14
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Display Color of Variables

    hi Chris,

    Here's a variation on Hans' demo. I think it does what you want.
    Attached Files Attached Files
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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