Monday, April 25, 2011

VB6 Changing colors for every control on a form

Hi,

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

From stackoverflow
  • 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.

    Belliez : 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 : 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 : I have done just that using the code from the answer below. However, I cannot seem to make User Controls work this way!
  • It's going back quite a few years now, but wasnt there a 'Transparent' background color?

    Set all the labels to have a transparent background, and you only need to set the form color once.

    MarkJ : That'd do labels but not other controls. http://msdn.microsoft.com/en-us/library/aa245036(VS.60).aspx The OP mentions text boxes & frames which don't support transparent
  • 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
    
    AnthonyWJones : @Tomalak: yes Typeof x Is y (where y is a type identifier) is valid in VB6, but you're too young to know that ;)
    Ferdeen : 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 : @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 : @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 : 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.
    AnthonyWJones : It would work if you use "Select Case True" then have each case expression as "Case TypeOf ctlThing Is CheckBox" etc.
    Belliez : thanks, I used your code and it worked great for forms, cant seem to get User Controls to change on the forms!
    Ferdeen : 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 : This will only affect loaded forms. It will only be effective if you load all the forms at app startup & keep them loaded. Otherwise you'll need to trigger the loop over Controls from the Form_Load event. Watch out for dynamically loaded controls too
  • 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

    Tomalak : +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.
    MarkJ : +1 . This is what we did in a similar situation. Might be worth mentioning that the FRM format is documented online. http://msdn.microsoft.com/en-us/library/aa716301(VS.60).aspx
  • Just for completeness...

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

    ~Mike~

0 comments:

Post a Comment