none
Custom Render for Toolstrip..How To Render A button that is selected RRS feed

  • Question

  •  

    So, I know how to create a custom render class for a toolstrip..BUT how do I render that a button is checked...for example: (that I got from a site somewheres)

     

    protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)

    {

    // Check if the item is selected or hovered over.

    if (e.Item.Selected || e.Item.Pressed)

    {

    LinearGradientBrush brush =

    new LinearGradientBrush(e.Item.Bounds,

    Color.DarkBlue,

    Color.DarkGreen, 90);

    e.Graphics.FillRectangle

    (brush, 0, 0, e.Item.Width, e.Item.Height);

    }

    }

     

    This renders the button great as I hover over and click..but when I click the button I want it to stay colored with what I want. I know the vaule is going true false but the color won't stick.

     

    Now I can use this:

     

    class CustomProfessionalColors : ProfessionalColorTable

    {

    public override Color ToolStripGradientBegin

    { get { return Color.Bisque; } }

    public override Color ToolStripGradientMiddle

    { get { return Color.SeaShell; } }

    public override Color ToolStripGradientEnd

    { get { return Color.CornflowerBlue; } }

    public override Color ButtonPressedHighlight

    {

    get

    {

    return Color.SkyBlue;

    }

    }

    public override Color ButtonCheckedHighlight

    {

    get

    {

    return Color.CornflowerBlue;

    }

    }

     

    //public override Color MenuStripGradientBegin

    //{ get { return Color.SkyBlue; } }

    //public override Color MenuStripGradientEnd

    //{ get { return Color.OrangeRed; } }

    }

     

     

    and just set my toolstrip manager globally

    ToolStripManager.Renderer = new ToolStripProfessionalRenderer(new CustomProfessionalColors());

     

     

    but then I can't check to see the text value of the button so that I can change a color based on what button it is..

     

    if button says HI i want the checked color to be bue.

    if it says BYE I want the checked color to be green.

     

    I am using buttons with text only, no image. and the can check property is set correctly...

     

    THANKS

     

     

    Tuesday, November 6, 2007 2:36 AM

Answers

  • Hi melegant

     

    The following code shows how to paint the buttons base on the buttons type.

     

    Code Block

    public class GreenToolStripButton : ToolStripButton

    {

    }

     

    public class BlueToolStripButton : ToolStripButton

    {

    }

     

    public class CustomToolStripRenderer : ToolStripProfessionalRenderer

    {

        protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)

        {

            // other codes...

     

            if (e.Item is GreenToolStripButton)

            {

                // paint checked button to green

            }

            else if (e.Item is BlueToolStripButton)

            {

                // paint checked button to blue

            }

     

            // other codes...

        }

    }

     

     

    Best Regards,

    Wei Zhou

    Monday, November 12, 2007 3:38 AM