Ich verwende Visual Studios, um eine Windows-Formularanwendung zu erstellen. Mein System ist verantwortlich für die Erstellung und Anzeige von Modulinformationen für Universitäten. Allerdings, wenn mein System bootet, füllt es momentan das Tool-Strip-Menü. Ich brauche eine Möglichkeit, neue Einträge zu dieser Liste hinzuzufügen, wenn sie erstellt werden. Die neuen Datensätze werden auf einem anderen Formular erstellt, und ich kann nicht herausfinden, wie Elemente zu diesem ToolStripDropdown auf Formular 1 von dem Erstellen von Form2 (siehe unten für wie es aussieht) hinzugefügt werden. Ich habe versucht, das ToolStripMenuItem einfach öffentlich zu machen, aber das funktioniert nicht. Kann mir jemand helfen?Hinzufügen eines Elements zu einer ToolStripMenuItem-Dropdownliste aus einem anderen Formular
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);
Aktueller Code für Elemente in die Drop-Zugabe nach unten:
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();
}
}
}
Sie müssen in der zweiten Form Referenz für die erste Form haben. So: http://stackoverflow.com/a/38460510/3185569 – user3185569