Resolved Why current theme colors are returned

aaaron

Well-known member
Joined
Jan 23, 2011
Messages
219
Programming Experience
10+
I have a method that does:
VB.NET:
nameAndColors = FillNameAndColors((New ToolStripProfessionalRenderer()).ColorTable)
which calls:
VB.NET:
Private Function FillNameAndColors(ProfessionalTableIn As ProfessionalColorTable) As StringWithColorSortStringOrColor()
        Dim nameAndColors As StringWithColorSortStringOrColor()
        Dim myRendererTableType As Type

        myRendererTableType = ProfessionalTableIn.GetType

        Dim propInfoList As Reflection.PropertyInfo()

        propInfoList = myRendererTableType.GetProperties()

        Dim numProps As Integer = propInfoList.Length

        ReDim nameAndColors(numProps - 1)

        For indexIntoProps As Integer = 0 To numProps - 1
Checking ToolStripProfessionalRenderer.cs I find:
C#:
public class ToolStripProfessionalRenderer : ToolStripRenderer
{
       private ProfessionalColorTable professionalColorTable;
With these constructors and I assue I called the top one:
C#:
public ToolStripProfessionalRenderer()
{
}

internal ToolStripProfessionalRenderer(bool isDefault) : base(isDefault)
{
}

public ToolStripProfessionalRenderer(ProfessionalColorTable professionalColorTable)
{
    this.professionalColorTable = professionalColorTable;
}
It also has ths methods. I assume since I haven't set professionalColorTable it is Null and ProfessionalColors.ColorTable is returned. I think this is also Null.
C#:
public ProfessionalColorTable ColorTable
{
    get
    {
        if (professionalColorTable == null)
        {
            return ProfessionalColors.ColorTable;
        }
        return professionalColorTable;
    }
}
Checking ProfessionalColors.cs I find:
C#:
private static ProfessionalColorTable professionalColorTable = null;

internal static ProfessionalColorTable ColorTable
{
    get
    {
        if (professionalColorTable == null)
        {
            professionalColorTable = new ProfessionalColorTable();
        }
        return (professionalColorTable
    }
}
Here again it appears that professionalColorTable is null so the method would return professionalColorTable which is Null. Bottom line is: I am callig FillNameAndColors with argunent equal to Nothing

However, my indexIntoProps loop in the method at the top seems to work fine and I seem to get a list of the color from whatever theme I have select in Windows.

Of course I've checked the argument with the debugger and it is a fine ProfessionalColorTable with reasonable arguments.

So I can not be passing Nothing as the above analysis would indicate.

Can you see where I've gone wrong above?

And why are current theme colors returned?
 
Last edited by a moderator:
Solution
ProfessionalColors shared constructor calls SetScheme, which gets VisualStyleInformation.ColorScheme, which calls GetCurrentThemeName.
For future reference, if you're posting multiple code snippets then please post them separately, especially if they have text between them and are different languages.
 
Can you see where I've gone wrong above?
The VB code you posted is incomplete. You also haven't explained what you expect to happen.
 
Actually it took me a little time to select what I thought was enough code to display the problem with out expecting too much of the reader.

Most of the code I used came from two MS source files. I'm sure more would be good but I don't yet know what would be best. Probably, if I did I'd have answered my question myself.

I probably simply made some dumb mistake in interpreting the constructors, at least that's what i expect any answer I get to this to show.

The following code is from one of my subs. FillNameAndColors uses the ColorTable to display the colors of the current theme. To my surprise.

VB.NET:
nameAndColors = FillNameAndColors((New ToolStripProfessionalRenderer()).ColorTable)

I don't remember why but I looked at the constructor for ToolStripProfessionalRenderer (code above) but I did.
It returned a professionalColorTable which when I looked at the code for that (code above) it appeared to me should be Nothing.

But as I just said, it is not. It is the current theme colors.
 
For future reference, if you're posting multiple code snippets then please post them separately, especially if they have text between them and are different languages.
I composed it off line and after posting saw it could have been better done.
 
ProfessionalColors shared constructor calls SetScheme, which gets VisualStyleInformation.ColorScheme, which calls GetCurrentThemeName.
 
Solution
ProfessionalColors shared constructor calls SetScheme, which gets VisualStyleInformation.ColorScheme, which calls GetCurrentThemeName.

Boils down to: I don't know C#. The constructors are actually the following, not the code I gave above.

Thanks

C#:
 static ProfessionalColors()
        {
            SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(OnUserPreferenceChanged);
            SetScheme();
        }

        private ProfessionalColors()
        {
        }
 
Last edited by a moderator:
The last snippet you posted is C#, not VB.Net.

1616249599718.png
 
Back
Top