VB6 Changing colors for every control on a form

13,651

Solution 1

You can do a for each and eliminate the controls you don't want.

Dim frmThing as Form    
Dim ctlThing as Control

For Each frmThing In Forms
  frmThing.BackColor = vbYellow
  For Each ctlThing In frmThing.Controls
    If (TypeOf ctlThing Is TextBox) Or _
    (TypeOf ctlThing Is CheckBox) Or _
    (TypeOf ctlThing Is ComboBox) Then
      ctlThing.BackColor = vbYellow
    End If
  Next
Next

Solution 2

The .frm files are simply standard ANSI text files. A background color property of a control would look like this:-

BackColor       =   &H80000005&

(Note the above is a system color but you can specify the RGB color using by using the lower 3 bytes and leaving the high byte 0).

A control such a Label would look like this:-

Begin VB.Label Label1 
  Caption         =   "Hello:"
  Height          =   285
  Left            =   90
  TabIndex        =   3
  Top             =   480
  Width           =   1305
End

So that task could be done lexically by parsing the .frm files and inserting (or replacing) the BackColor attribute line.

Edit:

Useful link posted in comments by MarkJ : Form Description Properties

Solution 3

you could do this at runtime by looping the Controls collection and setting the background of each. This would give you the flexibility of changing your theme.

You could also work through the source files, parse out the controls and enter/change the background colours that you want. This approach is probably more work, for less reward.

Solution 4

Just for completeness...

ssCheck does not have a BackColor property and will produce an error using the aforementioned methods

~Mike~

Share:
13,651
Belliez
Author by

Belliez

I'm a software developer and own a small PC / Laptop repair company called PCease.com. I love to program full time, part-time, in my own time or when I can....

Updated on November 19, 2022

Comments

  • Belliez
    Belliez over 1 year

    I am trying to change the colour theme of an old VB6 application (make it look a bit more modern!).

    Can someone tell me how I could change the backcolor of every control on a form without doing it for each and every control (label, button, frame etc!).

    I have about 50 forms, all containing such controls and doing this manually for each form in code would take an age!

    I am also open to better suggestions and ideas on how I can skin / theme a VB6 application?

    Thanks in advance

  • AnthonyWJones
    AnthonyWJones over 15 years
    @Tomalak: yes Typeof x Is y (where y is a type identifier) is valid in VB6, but you're too young to know that ;)
  • Syed Osama Maruf
    Syed Osama Maruf over 15 years
    I remember (back in the day) always having this sort of function on my VB projects. Instead of IF OR statements I would use CASE.
  • Tomalak
    Tomalak over 15 years
    @AnthonyWJones: I guess it must be the age. ;-) I was indeed unaware of the TypeOf... well, lets call it an "extension to the If statement". It's not an operator in it's own right, but it's nice to know.
  • AnthonyWJones
    AnthonyWJones over 15 years
    @Tomalak: Its not an extension of the If statement. The TypeOf .. Is .. construct can be used anywhere you can use a boolean expression. TypeOf x though would be a syntax error you can only do TypeOf .. Is ..
  • Tomalak
    Tomalak over 15 years
    I see... Too bad I have nothing but VBA documentation here - it's mentioned only in the If statement there. That would imply that using it in a Select Case statement (as Ferds seems to remember) would not work very well.
  • Tomalak
    Tomalak over 15 years
    +1 This would have been my "no code change required"-idea as well. I don't think developing a skinning mechanism is what the OP is after.
  • AnthonyWJones
    AnthonyWJones over 15 years
    It would work if you use "Select Case True" then have each case expression as "Case TypeOf ctlThing Is CheckBox" etc.
  • Belliez
    Belliez over 15 years
    I want to be able to do this at runtime so that I can set a single value for the colours in one place only. thanks
  • MrTelly
    MrTelly over 15 years
    In that case create a Theme class, pass each form to it when it's loaded, and put all your colour/theming logic in there. If you build it as a seperate COM DLL you could re-use and/or push it out to the community
  • Belliez
    Belliez over 15 years
    thanks, I used your code and it worked great for forms, cant seem to get User Controls to change on the forms!
  • Belliez
    Belliez over 15 years
    I have done just that using the code from the answer below. However, I cannot seem to make User Controls work this way!
  • Syed Osama Maruf
    Syed Osama Maruf over 15 years
    do you have access to the user control source code ? if so could you also add the code there ? or would that be a painful task ?
  • MarkJ
    MarkJ over 15 years
    +1 . This is what we did in a similar situation. Might be worth mentioning that the FRM format is documented online. msdn.microsoft.com/en-us/library/aa716301(VS.60).aspx