c#:如何在运行时添加下拉菜单项

发布于 2024-09-25 01:11:43 字数 1189 浏览 0 评论 0原文

创建新项目时,我希望可以从下拉列表中使用该项目。我创建的第一个项目可以工作,并且被放置为菜单项和下拉项,但是当我尝试添加下一个下拉项时,它崩溃并告诉我“对象引用未设置为对象的实例”。

此行创建异常: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖鼻大婶 2024-10-02 01:11:43

ToolStripMenuItem ProjectToolStripMenuItem = null;

应该在方法之外声明。很抱歉给您带来不便。

/托马斯

ToolStripMenuItem ProjectToolStripMenuItem = null;

Should have been declared outside the method. Sorry for the inconvinience.

/Thomas

久隐师 2024-10-02 01:11:43

您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文