0

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?

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

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();
        }
    }
}
GiggyLapisar
  • 105
  • 3
  • 10
  • You need to have reference for the first form in the second form. Like this: http://stackoverflow.com/a/38460510/3185569 – Zein Makki Jul 24 '16 at 12:15

1 Answers1

0

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);
Community
  • 1
  • 1
Paviel Kraskoŭski
  • 1,429
  • 9
  • 16