Windows Develop Bookmark and Share   
 index > Windows Forms General > ToolStripMenuItem questions
 

ToolStripMenuItem questions

I've got a couple of (hopefully) simple questions on using the ToolStripMenuItem in conjunction with a ContextMenu in VS 2005. First, I need to change the font color, not only when it's selected, but before it's selected. I need some menu items to be green and some to be blue. I also need to make my context menus support multiple select, so on any given menu or sub menu, the user can pick an item, and instead of the menu closing, that item gets checked and the user can select one or more until they hit an OK menu item which then closes the menu and moves up one level in the menu heirarchy.

I'm using C#.

And ideas on how to pull off the above without completely rewriting Context Menus (which is what I've done in the past)?
Dwarf  Wednesday, June 15, 2005 2:48 AM
Great questions, here are answers:

1. Font color:

There is a bug that got fixed yesterday where ForeColor wasn't being respected so it won't appear in the CTP builds for a while. Until you see that changed you need to override the Pro Renderer's OnRenderItemText() and set e.TextColor like so:


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ToolStripProfessionalRenderer tsp = new MyToolStripProfessionalRenderer();
            ToolStripManager.Renderer = tsp;
        }

    }

    class MyToolStripProfessionalRenderer: ToolStripProfessionalRenderer
    {
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            e.TextColor = Color.HotPink;
            base.OnRenderItemText(e);
        }

    }


 



after that, you'll just be able to set ForeColor.

2. MultiSelect ContextMenuStrip

Sync the closing (cancellable) event - check closereason and don't respect itemclicked, by returning true.


 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Items.Add("Apples");
            cms.Items.Add("Bananas");
            cms.Items.Add("Cherries");

            cms.Closing += cms_Closing;
            this.ContextMenuStrip = cms;

            foreach (ToolStripMenuItem tsmi in cms.Items)
            {
                tsmi.CheckOnClick = true;
            }

        }

        void cms_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
            {
               // could also check for specific "Ok" item in e.Item
                e.Cancel = true;
            }
        }


 

Erick Ellis  Thursday, June 16, 2005 12:38 AM
Great questions, here are answers:

1. Font color:

There is a bug that got fixed yesterday where ForeColor wasn't being respected so it won't appear in the CTP builds for a while. Until you see that changed you need to override the Pro Renderer's OnRenderItemText() and set e.TextColor like so:


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ToolStripProfessionalRenderer tsp = new MyToolStripProfessionalRenderer();
            ToolStripManager.Renderer = tsp;
        }

    }

    class MyToolStripProfessionalRenderer: ToolStripProfessionalRenderer
    {
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            e.TextColor = Color.HotPink;
            base.OnRenderItemText(e);
        }

    }


 



after that, you'll just be able to set ForeColor.

2. MultiSelect ContextMenuStrip

Sync the closing (cancellable) event - check closereason and don't respect itemclicked, by returning true.


 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Items.Add("Apples");
            cms.Items.Add("Bananas");
            cms.Items.Add("Cherries");

            cms.Closing += cms_Closing;
            this.ContextMenuStrip = cms;

            foreach (ToolStripMenuItem tsmi in cms.Items)
            {
                tsmi.CheckOnClick = true;
            }

        }

        void cms_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked)
            {
               // could also check for specific "Ok" item in e.Item
                e.Cancel = true;
            }
        }


 

Erick Ellis  Thursday, June 16, 2005 12:38 AM
Erick, a couple of questions. 

1. ToolStripDropDownClosingEventArgs doesn't have an Item in Beta 2 (for telling which item was clicked, to see if it is OK to cancel as you suggested)...is this a recent addition?  Or do you have to grab that info in the ItemClicked event?

2. A regular ToolStripMenuStrip (not a ContextMenuStrip) doesn't have a DropDownClosing event, so you can't take the same approach for regular menus.  Will the DropDownClosing event be added?
durstin  Thursday, June 16, 2005 11:34 PM
Good catch, nope, ToolStripDropDownClosingEventArgs does not and will not have an item exposed. My mistake. You'd have to grab that in Item.Click, ItemClicked, etc.

Is there a DropDownClosing on MenuStrip? We just (6/15) added a MenuStrip.Deactivate event to oppose MenuStrip.Activate. These are not cancellable and are designed to parallel Form.MenuStart/MenuComplete.
Erick Ellis  Friday, June 17, 2005 2:35 PM

You can use google to search for other answers

Custom Search

More Threads

• user interface
• Help with a generic RtlMoveMemory
• empty UI message queue on separate thread
• BUG: ListView columns do not scale with DPI or Font changes
• Data Type
• How can I have 2 menu dropdown open at one time?
• navigating the contents of a combobox
• InitializeComponent method
• Outlookbar out of toolstrip items?
• Random Numbers...What am I not getting here?