【问题标题】:Adding an item to a ToolStripMenuItem drop down list from another form从另一个表单将项目添加到 ToolStripMenuItem 下拉列表
【发布时间】: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();
        }
    }
}

【问题讨论】:

标签: c# winforms toolstrip


【解决方案1】:

我在这里回答了 (https://stackoverflow.com/a/32916403/5378924),如何将一些控件从一个表单添加到另一个表单。

表格1:

public partial class Form1 : Form
{
     public static Form1 Instance { get; private set; }

     private void Form1_Load(object sender, EventArgs e)
     {
          Instance = this;
     }
}

表格2:

ToolStripItem newDropDownItem = new ToolStripMenuItem();
newDropDownItem.Text = newmodcode;
Form1.Instance.modulesToolStripMenuItem.DropDownItems.Add(newDropDownItem);

【讨论】:

  • 这样,在打开Form1的下一个实例后,Instance属性将指向新的表单。
  • 看看these options,你会发现它们很有用:)
  • @RezaAghaei 我认为,Form1 将永远存在,直到用户关闭应用程序。 Form1 是应用程序的起点,它只打开和关闭一次。
猜你喜欢
  • 1970-01-01
  • 2010-11-09
  • 2016-09-23
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 2014-07-23
相关资源
最近更新 更多