I am creating a modular note taking system and I need a way to create a new item in a drop down menu on the main form while also creating an event handler for this new item. I have already created a handler for activation on initialisation but I need to be able to invoke it from another form. I have included the code for both forms and have blockquoted the code from Form1 which is particularly relevant.
So I need to be able to add a new item to a dropdown menu on Form1 and then have the system create a new event handler for if this new item is clicked.
If images of the form are needed let me know. Hopefully someone will be able to help.
Form 1(The Main Form)
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 Form1 : Form
{
//Defines variables
public static string moduleCod;
public static string[] codRetr = Directory.GetFiles(".", "*code.txt");
public static List<string> notes;
public static int i = 0;
public static string a = "a";
public static List<string> codDrop;
public static string codDropT;
public string notePath;
public string noteLoad = Path.GetFullPath("noteLoad.txt");
public string notePathFind;
public static string modNoLocation;
public static string modNo;
public static string modTitle;
public static string modTitleLocation;
public static string modSynopsis;
public static string modSynLocation;
public static string modLOs;
public static string modLOsLocation;
public static string modAssign;
public static string modAssignLoc;
public static string notesLoc;
public static string cod;
public Form1()
{
InitializeComponent();
**List<string> codDrop = new List<string>(); //codDrop.Clear(); i = 0; foreach (string s in codRetr) { codRetr[i] = codRetr[i].Remove(codRetr[i].Length - 8); cod = codRetr[i].Remove(0, 2); codDrop.Add(cod); i++; } i = 0; foreach (string t in codDrop) { ToolStripItem newDropDownItem = new ToolStripMenuItem(); newDropDownItem.Text = t; newDropDownItem.Click += new EventHandler(moduleChoice_Click); modulesToolStripMenuItem.DropDownItems.Add(newDropDownItem); }**
}
private void moduleChoice_Click(object sender, System.EventArgs e)
{
ToolStripItem menuItemThatHasBeenClicked = (ToolStripItem)sender;
listBox1.Items.Clear();
moduleCod = menuItemThatHasBeenClicked.Text;
notesLoc = "*" + moduleCod + "N.txt";
modNoLocation = moduleCod + "code.txt";
modNo = File.ReadAllText(modNoLocation);
ModuleCode.Text = modNo;
modTitleLocation = moduleCod + "title.txt";
modTitle = File.ReadAllText(modTitleLocation);
Title.Text = modTitle;
modSynLocation = moduleCod + "synopsis.txt";
modSynopsis = File.ReadAllText(modSynLocation);
ModuleSummary.Text = modSynopsis;
modLOsLocation = moduleCod + "LOs.txt";
modLOs = File.ReadAllText(modLOsLocation);
LOsTextbox.Text = modLOs;
modAssignLoc = moduleCod + "assignments.txt";
modAssign = File.ReadAllText(modAssignLoc);
AssignBox.Text = modAssign;
string absolutePath = Path.GetFullPath(@"./");
string[] noteFiles = Directory.GetFiles(".", notesLoc);
//List<string> noteFilesList = new List<string>();
foreach(string s in noteFiles)
{
listBox1.Items.Add(s);
listBox1.Click += new EventHandler(noteChoice_Click);
}
//listBox1.Items.Clear();
//listBox1.DataSource = noteFilesList;
}
private void noteChoice_Click(object sender, EventArgs e)
{
}
private void programmingAndDataStructureToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
modLOs = LOsTextbox.Text;
File.WriteAllText(modLOsLocation, modLOs);
}
private void ModuleSummary_TextChanged(object sender, EventArgs e)
{
modSynopsis = ModuleSummary.Text;
File.WriteAllText(modSynLocation, modSynopsis);
}
private void AssignBox_TextChanged(object sender, EventArgs e)
{
modAssign = AssignBox.Text;
File.WriteAllText(modAssignLoc, modAssign);
}
private void modulesToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
;
}
private void modulesToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void modulesToolStripMenuItem_DropDownOpened_1(object sender, EventArgs e)
{
}
private void modulesToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
}
private void Title_TextChanged(object sender, EventArgs e)
{
modTitle = Title.Text;
File.WriteAllText(modTitleLocation, modTitle);
}
private void AddNewModule_Click_1(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
//Manages deleting of modules
private void deleteCurrentModuleToolStripMenuItem_Click_1(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to delete this module?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
File.Delete(modNoLocation);
File.Delete(modTitleLocation);
File.Delete(modSynLocation);
File.Delete(modLOsLocation);
File.Delete(modAssignLoc);
moduleCod = "";
Title.Text = "";
LOsTextbox.Text = "";
ModuleCode.Text = "";
ModuleSummary.Text = "";
AssignBox.Text = "";
}
}
private void button1_Click_1(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
}
}
}
Form 2 (The Add a new Module Form)
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 = t;
newDropDownItem.Click += new EventHandler(Form1.moduleChoice_Click);
Form1 modulesToolStripMenuItem.DropDownItems.Add(newDropDownItem);
Close();
}
//Simple cancelation sequence closing the entry form
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
}