本文介绍了以编程方式将项目添加到菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我创建菜单条

here i create the menu strip

mnuMain = new MenuStrip();
Controls.Add(mnuMain);
mnuFile = new ToolStripMenuItem("File");
mnuFileNew = new ToolStripMenuItem("New");



但我想以一种方式创建,当我通过任何项目时,它将添加到菜单中

在这里,我必须为每个项目创建一个对象

例如



but i want to create in such a way that when i pass any item it will added to menu

here i have to create an object for each item

e.g

string items[10]
for(i=0;i>10;1++)
{
    mnufile = new ToolStripMenuItem(items[i]);
}



我的显示应该像这样的即时贴菜单
item1
item2
item3
item4
.
.
.
.
.
item9

您如何为此提供帮助?



my display should be like this im menu strip
item1
item2
item3
item4
.
.
.
.
.
item9

how for this u can help me

推荐答案

private void CreateMenuStrip()
        {
            MenuStrip menuStrip1 = new MenuStrip();
            ToolStripMenuItem fileToolStripMenuItem = new ToolStripMenuItem();
            ToolStripMenuItem openToolStripMenuItem = new ToolStripMenuItem();
            ToolStripMenuItem saveToolStripMenuItem = new ToolStripMenuItem();
            ToolStripSeparator toolStripMenuItem1 = new ToolStripSeparator();
            ToolStripMenuItem settingsToolStripMenuItem = new ToolStripMenuItem();
            ToolStripMenuItem displaySettingsToolStripMenuItem = new ToolStripMenuItem();
            ToolStripMenuItem otherSettingsToolStripMenuItem = new ToolStripMenuItem();
            //
            // displaySettingsToolStripMenuItem
            //
            displaySettingsToolStripMenuItem.Name = "displaySettingsToolStripMenuItem";
            displaySettingsToolStripMenuItem.Size = new System.Drawing.Size(157, 22);
            displaySettingsToolStripMenuItem.Text = "Display Settings";
            //
            // otherSettingsToolStripMenuItem
            //
            otherSettingsToolStripMenuItem.Name = "otherSettingsToolStripMenuItem";
            otherSettingsToolStripMenuItem.Size = new System.Drawing.Size(157, 22);
            otherSettingsToolStripMenuItem.Text = "Other Settings";
            //
            // settingsToolStripMenuItem
            //
            settingsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
            displaySettingsToolStripMenuItem,
            otherSettingsToolStripMenuItem});
            settingsToolStripMenuItem.Name = "settingsToolStripMenuItem";
            settingsToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            settingsToolStripMenuItem.Text = "Settings";

            //
            // fileToolStripMenuItem
            //
            fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
            openToolStripMenuItem,
            saveToolStripMenuItem,
            toolStripMenuItem1,
            settingsToolStripMenuItem});
            fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            fileToolStripMenuItem.Text = "File";
            //
            // openToolStripMenuItem
            //
            openToolStripMenuItem.Name = "openToolStripMenuItem";
            openToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            openToolStripMenuItem.Text = "Open";
            //
            // saveToolStripMenuItem
            //
            saveToolStripMenuItem.Name = "saveToolStripMenuItem";
            saveToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            saveToolStripMenuItem.Text = "Save";
            //
            // toolStripMenuItem1
            //
            toolStripMenuItem1.Name = "toolStripMenuItem1";
            toolStripMenuItem1.Size = new System.Drawing.Size(149, 6);

            menuStrip1.Items.AddRange(new ToolStripItem[] {
            fileToolStripMenuItem});
            menuStrip1.Location = new System.Drawing.Point(0, 0);
            menuStrip1.Name = "menuStrip1";
            menuStrip1.Size = new System.Drawing.Size(284, 24);
            menuStrip1.TabIndex = 0;
            menuStrip1.Text = "menuStrip1";
            Controls.Add(menuStrip1);
        }


string items[10] = SomeMethodToMakeMenuItems();
for(int i = 0; i > 10; i++)
{
  ToolStripMenuItem newItem = new ToolStripMenuItem(items[i]);
  mnuFile.DropDownItems.Add(newItem);
}


private void CreateMenuStrip()
        {
            MenuStrip menuStrip1 = new MenuStrip();
            ToolStripMenuItem fileToolStripMenuItem = new ToolStripMenuItem();
            ToolStripItem[] items = null;
            for (int i = 1; i < 10; i++)
            {
                ToolStripMenuItem NewStrip = new ToolStripMenuItem();
                NewStrip.Name = "item" + i.ToString();
                NewStrip.Size = new System.Drawing.Size(152, 22);
                NewStrip.Text = "item" + i.ToString();
                NewStrip.Click += new EventHandler(NewStrip_Click);
                if (items == null)
                    items = new ToolStripItem[1];
                else
                {
                    ToolStripItem[] Temp = new ToolStripItem[items.Length];
                    for (int j = 0; j < items.Length; j++)
                    {
                        Temp[j] = items[j];
                    }
                    items = new ToolStripItem[items.Length + 1];
                    for (int j = 0; j < Temp.Length; j++)
                    {
                        items[j] = Temp[j];
                    }
                }

                items[items.Length - 1] = NewStrip;
            }

            //
            // fileToolStripMenuItem
            //
            fileToolStripMenuItem.DropDownItems.AddRange(items);
            fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            fileToolStripMenuItem.Text = "File";

            menuStrip1.Items.AddRange(new ToolStripItem[] {
            fileToolStripMenuItem});
            menuStrip1.Location = new System.Drawing.Point(0, 0);
            menuStrip1.Name = "menuStrip1";
            menuStrip1.Size = new System.Drawing.Size(284, 24);
            menuStrip1.TabIndex = 0;
            menuStrip1.Text = "menuStrip1";
            Controls.Add(menuStrip1);
        }
        void NewStrip_Click(object sender, EventArgs e)
        {
            MessageBox.Show(((ToolStripMenuItem)sender).Name);
        }


这篇关于以编程方式将项目添加到菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:22