?
Solved

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

Posted on 2014-09-17
4
Medium Priority
?
81 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 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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