Solved

Is there an accepted method of looping thru MenuItems w/ToolStripSeparators?

Posted on 2014-09-17
4
79 Views
Last Modified: 2014-09-18
I have a dynamic menu structure associated with my Windows application. The main menu has subitems, ToolStripMenuItems along with ToolStripSeparators. The issue I'm having is knowing when a User's access is limited, certain menuitems won't show, which is correct, however, I not always able to prevent the ToolStripSeparator from showing. Is there a way to ensure I can remove ToolStripSeparators along with a ToolStripMenuItem if it's not suppose to show up?

I've attached an image for visual reference.

Hope that makes sense!

Thanks,
Screenshot.jpg
0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 500 total points
ID: 40328458
This method hides the unnecessary separators.
    private void CheckItems()
    {
        ToolStripItem lastSeparator = null;

        // Is there an available menu item since last separator ?
        bool availableItem = false;

        // Let 'dd' be your dropdownitem
        var dd = (ToolStripDropDownItem)toolStrip1.Items[0];
        foreach (ToolStripItem item in dd.DropDownItems)
        {
            if (item is ToolStripSeparator)
            {
                lastSeparator = item;
                if (availableItem)
                {
                    item.Available = true;
                    availableItem = false;
                }
                else
                {
                    item.Visible = false;
                }
            }
            else
            {
                if (item.Available)
                    availableItem = true;
            }
        }
        if (!availableItem) lastSeparator.Available = false;
    }

Open in new window

0
 

Author Comment

by:BlakeMcKenna
ID: 40328677
My bad...I meant a MenuStrip control with it's sub-menuitems...
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40329834
The same algorithm should work. Replace
        // Let 'dd' be your dropdownitem
        var dd = (ToolStripDropDownItem)toolStrip1.Items[0];

Open in new window

with
        // Let 'dd' be your menuitem
        var dd = (ToolStripMenuItem)menuStrip1.Items[0];

Open in new window

0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40330192
Thank you!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question