Question

I have a inserted items in the toolstripmenuitem control successfully. But i am not able to rearrange items as shown below :-

For ex :- Currently Items are added in toolstripmenuitem like below fashion:-

aa bb cc dd
ee ff gg hh

But i want like this :-

aa ee
bb ff
cc gg
dd hh

My code is :-

private void CreateEnterpriseHierarchy(ToolStripMenuItem selectToolStripMenuItem)
    {
        try
        {
            ToolStripMenuItem entToolStripMenuItem = new ToolStripMenuItem();
            var enterprise = CurrentUserContext.ECPTree.Where(x => x.ImageIndex == 0).FirstOrDefault();
            if (enterprise != null)
            {
                entToolStripMenuItem.Tag = enterprise;
                entToolStripMenuItem.ToolTipText = entToolStripMenuItem.Name = entToolStripMenuItem.Text = enterprise.Name;
                //entToolStripMenuItem.Enabled = enterprise.IsNodeEnabled;
                //newToolStripMenuItem.Image = 
                entToolStripMenuItem.Image = this.imgListMenu.Images[0];
                CreateCompanyList(entToolStripMenuItem, enterprise);
            }
            else
            {
                entToolStripMenuItem.Text = "---------";
            }

            // To open Hierarchy of Enterprise - Companies and Properties with Multiple Columns

            ToolStripDropDown menu = new ToolStripDropDown();
            ToolStripItem[] items = new ToolStripItem[entToolStripMenuItem.DropDown.Items.Count];
            entToolStripMenuItem.DropDown.Items.CopyTo(items, 0);

            // Transfer the items into the drop-down menu.
            foreach (ToolStripItem item in items)
            {
                if (!(item is ToolStripSeparator)) menu.Items.Add(item);
            }

            // Adjust the layout of the new menu.
            menu.LayoutStyle = ToolStripLayoutStyle.Table;
            ((TableLayoutSettings)menu.LayoutSettings).ColumnCount = 6;

            // Attach it to the menu.
            entToolStripMenuItem.DropDown = menu;

            selectToolStripMenuItem.DropDownItems.Add(entToolStripMenuItem);
        }
        catch (Exception ex)
        {
            CusException cex = new CusException(ex);
            cex.Show(MessageBoxIcon.Error);
        }
    }

and the Method CreateCompanyList:-

void CreateCompanyList(ToolStripMenuItem selectedMenuItem, SCTreeView Enterprise)
    {
        try
        {
            ToolStripMenuItem enterpiseMenuItem = new ToolStripMenuItem();
            enterpiseMenuItem.Text = selectedMenuItem.Text;
            enterpiseMenuItem.Tag = selectedMenuItem.Tag;
            enterpiseMenuItem.Name = enterpiseMenuItem.Text = selectedMenuItem.Name;
            enterpiseMenuItem.Enabled = Enterprise.IsNodeEnabled;
            enterpiseMenuItem.ImageIndex = 0;
            enterpiseMenuItem.Image = this.imgListMenu.Images[0];
            enterpiseMenuItem.Click += new EventHandler(DisplaySelectedLevel);
            selectedMenuItem.DropDownItems.Add(enterpiseMenuItem);
            selectedMenuItem.DropDownItems.Add(new ToolStripSeparator());
            foreach (var item in CurrentUserContext.ECPTree)
            {
                if (item.ImageIndex == 1)
                {
                    ToolStripMenuItem companyMenuItem = new ToolStripMenuItem();
                    companyMenuItem.Tag = item;
                    companyMenuItem.Name = companyMenuItem.Text = item.Name;
                    // companyMenuItem.Enabled = item.IsNodeEnabled;
                    var lstProperties = CurrentUserContext.ECPTree.Where(x => x.ParentID == item.ID).ToList();
                    if (lstProperties.Count == 0)
                    {
                        companyMenuItem.Click += new EventHandler(DisplaySelectedLevel);
                    }
                    companyMenuItem.ImageIndex = item.ImageIndex;
                    companyMenuItem.Image = this.imgListMenu.Images[1];
                    selectedMenuItem.DropDownItems.Add(companyMenuItem);

                    CreatePropertyList(item, companyMenuItem);
                }
            }
            if (selectedMenuItem.DropDown.Height > 500)
            {
                selectedMenuItem.DropDown.AutoSize = true;
                selectedMenuItem.DropDown.Height = 500;
            }
            selectedMenuItem.DropDownDirection = ToolStripDropDownDirection.Default;
        }
        catch (Exception ex)
        {
            CusException cex = new CusException(ex);
            cex.Show(MessageBoxIcon.Error);
        }
    }

Is there some way to achieve the desired layout style ?

Any help or idea is appreciated.

Was it helpful?

Solution

Standard .Net menu bar is a little bit messy especially when it comes to vertical displays.

Apply these values to your main menu bar properties:

  • TextDirection: Vertical90
  • LayoutStyle: VerticalStackWithOverflow
  • Dock: Left

This will make your menu bar bigger than what you expected, but it is relied on its items width. When you want to add your menu remember to specify its width.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top