【发布时间】:2016-07-24 12:11:05
【问题描述】:
我正在使用 Visual Studios 创建一个 Windows 窗体应用程序。我的系统负责为大学创建和查看模块信息。但是,目前虽然当我的系统启动时它会填充工具条菜单,但我需要一种在创建新项目时将它们添加到此列表的方法。新记录是在不同的表单上制作的,我无法弄清楚如何从创建 Form2 的表单 1 上的 ToolStripDropdown 中添加项目(请参阅下面的外观)。我试过简单地将 ToolStripMenuItem 简单地公开,但这不起作用。谁能帮帮我?
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();
}
}
}
【问题讨论】:
-
您需要在第二个表格中有第一个表格的参考。像这样:stackoverflow.com/a/38460510/3185569