• Treeview Background Color (Access 2007)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Treeview Background Color (Access 2007)

    Author
    Topic
    #455824

    Anyone have any idea if the background color of a treeview can be set via vba code? And, if so, can you point me to or provide an example.

    Viewing 0 reply threads
    Author
    Replies
    • #1136197

      The treeview control has no built-in support for this; you need to use Windows API functions.

      Copy the code from the attached text file (source: VB Helper Tutorial: Background Color) into a new standard module, and use a line like the following in the On Load event of the form with the treeview control:

      ChangeBackgroundColor Me.Treeview1.hWnd, vbRed

      Replace Treeview1 with the name of the treeview control and vbRed with the color that you want to use. You can use a symbolic constant for a literal color such as vbRed, a system color such as vbInfoBackground, or an RGB value such as RGB(255, 0, 128).

      Important: treeview nodes are *not* transparent, so you have to set the BackColor property of the nodes too.

      • #1136684

        Hans,

        Thanks much. However, there must be one more consideration I’m overlooking. I have a three level tree and, between your code and setting the BackColor as I build each node level, the BackColor seems to work with the exception of the last items at each level. eg ( The last Level 1 node doesn’t have the color set in the narrow band/column under its + for each Level 2 item. Similarly, the last Level 2 node under each Level 1 node has the same effect, the color is not set under its + for each Level 3 item. And finally, the last Level 3 node under each Level 2 has the same no-color problem for each item.) My sequence for the Treeview is: set the treeview properties, create Level 1 node items with BackColor, create Level 2 nodes with BackColor, create Level 3 nodes with BackColor and finally set the Treeview BackColor with your code. Thinking it might be a timing issue, I’ve tried moving the ChangeBackgroundColor to before and aslo after the setting of its general properties … no difference.

        Appreciate any insights you have,
        Marty

        • #1136741

          Using Windows API functions is a “dirty” trick – the treeview control was not designed with a non-standard background color in mind, so it behaves unpredicatably if you set the background color anyway. I think you’ll have to choose between not setting the background color (which would be my preference) and living with the quirks if you do.

          • #1136960

            Was hoping your response would be to set a property, fix the sequence or some such thing. It’ll be up to the client on whether to BackColor or not.

            Thanks again for your help,
            Marty

    Viewing 0 reply threads
    Reply To: Treeview Background Color (Access 2007)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: