Solved

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

Posted on 2014-09-17
4
74 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
  • 2
  • 2
4 Comments
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 500 total points
Comment Utility
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
Comment Utility
My bad...I meant a MenuStrip control with it's sub-menuitems...
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
Comment Utility
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
Comment Utility
Thank you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now