Roberto D V Leonardo
Roberto D V Leonardo

Reputation: 69

ToolStripProfessionalRenderer to override appearance dynamically (C#, Winforms)

I tried searching but found nothing about this specific need. I'm handling the text color of my menu items with this code

    private class SkinRenderer : ToolStripProfessionalRenderer
    {
        public SkinRenderer(MenuColorTable colorTable) : base(colorTable) => Console.WriteLine("test");
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            e.TextColor = Color.FromArgb(220, 220, 220);
            TextRenderer.DrawText(e.Graphics, e.Text, e.TextFont, e.TextRectangle, e.TextColor, e.TextFormat);
        }
    }

Aside mentioning that I don't actually know how to handle the first line inside this code (I only know it's necessary to handle a custom MenuColorTable but I don't understand why I must mandatorily state some code instruction), what I need to do mainly in this topic is setting the text color based on some conditions but I don't get how to do that. My first take is like, when I need to change that, I'll use another ToolStripProfessionalRenderer with a different text color but it seems a bit stupid to me. I mean, must I really do that? I'd rather set the text in some way like

e.TextColor = intVariable > 10 ? Color.Black : Color.White;

but it seems I cannot use this kind of statement because anything that's in the form cannot be used inside this method. Maybe it's something about how the classes are declared? I don't know if that's the case. I also tried passing some parameter to use but I didn't manage to do that, I'm also not finding so many clear instructions about how to pass parameters for classes (something like a parameter to use in Public Sub New() in VB.Net). What I have (showing only the structure for understanding purposes) is:

public partial class Form1 : Form
{
    private class SkinRenderer : ToolStripProfessionalRenderer
    {
        override method
    }
}

So basically the condition to use for the text is inside the form but outside the class and I don't know how to do that. Also, but I think it's important to start understanding from this very step, if I can handle the text, I can handle dynamically also the other components of the menu. So, first, I'm focusing just on this basic task.

I remembered also this could be useful to read. I'm setting the renderer like this in the load event of the form:

menu.Renderer = new SkinRenderer(new MenuColorTable());

The color table is another class that I suppose is not needed to see for the text purpose.

Upvotes: 0

Views: 66

Answers (0)

Related Questions