在Toolstripmenuitem中以升序和垂直方式插入菜单项

时间:2013-10-18 07:40:24

标签: c# toolstripdropdown toolstripmenu toolstripitem

我在toolstripmenuitem控件中成功插入了一个项目。但我无法重新排列如下所示的项目: -

对于前: - 目前项目添加在toolstripmenuitem中,如下所示: -

aa bb cc dd
ee ff gg hh

但我想这样: -

aa ee
bb ff
cc gg
dd hh

我的代码是: -

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);
        }
    }

和方法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);
        }
    }

有没有办法实现所需的布局风格?

感谢任何帮助或想法。

1 个答案:

答案 0 :(得分:0)

标准.Net菜单栏有点乱,特别是在垂直显示方面。

将这些值应用于主菜单栏属性:

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

这会使您的菜单栏比预期的更大,但依赖于其项目width。如果要添加菜单,请记住指定其宽度。