简体   繁体   中英

Adding an item to a ToolStripMenuItem drop down list from another form

I am using Visual Studios to create a Windows form application. My system is in charge of creating and viewing module information for universities. However, currently though when my system boots it will fill the Tool Strip Menu I need a way of adding new items to this list when they are created. The new records are made on a different form and I cannot figure out how to add items to this ToolStripDropdown on Form 1 from the creating one of Form2 (see below for what it looks like). I have tried simply making the ToolStripMenuItem simply public but that does not work. Can anyone help me out?

作为ToolStrip1的存在的ToolStrip

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

Current code for adding items to the Drop down:

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

I answered here ( https://stackoverflow.com/a/32916403/5378924 ), how to add some controls from one form to another.

Form1:

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

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

Form2:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM