c#:如何在运行时添加下拉菜单项
创建新项目时,我希望可以从下拉列表中使用该项目。我创建的第一个项目可以工作,并且被放置为菜单项和下拉项,但是当我尝试添加下一个下拉项时,它崩溃并告诉我“对象引用未设置为对象的实例”。
此行创建异常: ProjectToolStripMenuItem.DropDownItems.Add(newproject);
ArrayList projects = new ArrayList();
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
Project moldProject = new Project();
moldProject.ProjectName = "unnamed";
ToolStripMenuItem ProjectToolStripMenuItem = null;
if (projects.Count == 0)
{
ProjectToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
ProjectToolStripMenuItem.Name = "projectToolStripMenuItem";
ProjectToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
ProjectToolStripMenuItem.Text = "unnamed*";
this.menuStrip1.Items.Add(ProjectToolStripMenuItem);
}
ToolStripMenuItem newproject = new System.Windows.Forms.ToolStripMenuItem();
string name = "aboutToolStripMenuItem" + (projects.Count + 1).ToString();
newproject.Name = name;
newproject.Size = new System.Drawing.Size(211, 22);
newproject.Text = "unnamed*";
ProjectToolStripMenuItem.DropDownItems.Add(newproject);
projects.Add(moldProject);
}
When creating a new project, i want this project to become available from a dropdown. The first project i create, works, and is placed as the menu item as well as a dropdown item, however when i try to add the next dropdown item it crashes and tells me "Object reference not set to an instance of an object."
This line creates the exception:
ProjectToolStripMenuItem.DropDownItems.Add(newproject);
ArrayList projects = new ArrayList();
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
Project moldProject = new Project();
moldProject.ProjectName = "unnamed";
ToolStripMenuItem ProjectToolStripMenuItem = null;
if (projects.Count == 0)
{
ProjectToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
ProjectToolStripMenuItem.Name = "projectToolStripMenuItem";
ProjectToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
ProjectToolStripMenuItem.Text = "unnamed*";
this.menuStrip1.Items.Add(ProjectToolStripMenuItem);
}
ToolStripMenuItem newproject = new System.Windows.Forms.ToolStripMenuItem();
string name = "aboutToolStripMenuItem" + (projects.Count + 1).ToString();
newproject.Name = name;
newproject.Size = new System.Drawing.Size(211, 22);
newproject.Text = "unnamed*";
ProjectToolStripMenuItem.DropDownItems.Add(newproject);
projects.Add(moldProject);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ToolStripMenuItem ProjectToolStripMenuItem = null;
应该在方法之外声明。很抱歉给您带来不便。
/托马斯
ToolStripMenuItem ProjectToolStripMenuItem = null;
Should have been declared outside the method. Sorry for the inconvinience.
/Thomas
您的 ProjectToolStripMenuItem 设置为 null,并且仅当projects.Count 等于零时才会为其分配值。这就是为什么您会收到第二个项目的空引用异常。由于projects.Count不为零,ProjectToolStripMenuItem永远不会被分配。
Your ProjectToolStripMenuItem is set to null and it only gets assigned a value if the projects.Count is equal to zero. This is why you get the null reference exception for the second project. Since projects.Count is not zero, ProjectToolStripMenuItem never gets assigned.