Solved

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

Posted on 2014-09-17
4
77 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 38
Visual Studios 1 76
Very Large data in MYSQL 7 73
Import MySQL data into MS Access using VB.Net interface 5 29
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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