将项添加到另一个表单的ToolStripMenuItem下拉列表中

时间:2016-07-24 12:11:05

标签: c# winforms toolstrip

我正在使用Visual Studios来创建Windows表单应用程序。我的系统负责为大学创建和查看模块信息。但是,目前虽然我的系统启动它将填充工具条菜单我需要一种在创建它们时将新项添加到此列表的方法。新记录是以不同的形式制作的,我无法弄清楚如何在创建Form2之一时向表单1上的ToolStripDropdown添加项目(请参阅下面的内容)。我试过简单地将ToolStripMenuItem简单地公开,但这不起作用。任何人都可以帮助我吗?

The ToolStrip as present of ToolStrip1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Mod_Note_2._0
{
    public partial class Form2 : Form
    {
        public string newmodcode;
        public string baseText = "Enter information related to the module section here";

        public string newmodtitle;
        public string newmodsyn;
        public string newmodlo;
        public string newmodassign;

        public Form2()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            newmodcode = NewModuleCode.Text;

            //Adds the file path and name to the module code to create the file names
            newmodtitle = newmodcode + "title.txt";
            newmodsyn = newmodcode + "synopsis.txt";
            newmodlo = newmodcode + "LOs.txt";
            newmodassign = newmodcode + "assignments.txt";

            //Adds the file path the new module code so it can create the file
            string newmodcodepath = newmodcode + "code.txt";

            //Creates the new files with the filler text as default
            File.WriteAllText(newmodcodepath, newmodcode);
            File.WriteAllText(newmodtitle, baseText);
            File.WriteAllText(newmodsyn, baseText);
            File.WriteAllText(newmodlo, baseText);
            File.WriteAllText(newmodassign, baseText);

将项目添加到下拉菜单的当前代码:

ToolStripItem newDropDownItem = new ToolStripMenuItem();
                newDropDownItem.Text = newmodcode;

                Form1.modulesToolStripMenuItem.DropDownItems.Add(newDropDownItem);
            Close();
        }

        //Simple cancelation sequence closing the entry form
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在这里回答(https://stackoverflow.com/a/32916403/5378924),如何将一些控件从一种形式添加到另一种形式。

Form1中:

public partial class Form1 : Form
{
     public static Form1 Instance { get; private set; }

     private void Form1_Load(object sender, EventArgs e)
     {
          Instance = this;
     }
}

窗体2:

ToolStripItem newDropDownItem = new ToolStripMenuItem();
newDropDownItem.Text = newmodcode;
Form1.Instance.modulesToolStripMenuItem.DropDownItems.Add(newDropDownItem);