Solved

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

Posted on 2014-09-17
4
78 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
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Optimize the query 5 50
VB.NET Inline If statement 4 38
VB.net Filesystem watcher not working 5 31
Copying from a network share 3 19
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…

685 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