C#父表单和子表单之间的通信

时间:2015-04-15 19:00:06

标签: c# forms datagridview

我的项目有两个班级。第一类有关于大陆的信息,它还包含国家对象列表(另一类)。

我还宣布了一份包含所有大陆的大陆名单。 我已经成功地从文件中填充了列表,并成功地以相同的形式在DataGridView中显示它们。但问题是我没有找到一种方法来显示包含DataGridView的子表单。

那么,我如何将大陆列表转移到子表单中,以便我能够在其中显示它们?

我尝试了序列化和反序列化,但它没有用,我只看到了大陆班级成员的名字而没有别的。

以下是显示子表单的两个类和工具条代码:

// first class of continent
namespace WindowsFormsApplication1
{
   [Serializable]
     class continent
    {
        //champs

        private string nomc;
        public string Nomc
        {
            get { return this.nomc; }
        }


        private string sup;//SUP    
        public string Superficie
        {
            get { return this.sup; }
            set { this.sup = value; }
        }


        private string pop;//POP   
        public string Population
        {
            get { return this.pop; }
            set { this.pop = value; }
        }


        private string dens;//DENS  : 
        public string Densité
        {
            get { return this.dens; }
            set { this.dens = value; }
        }



        private string nbp;//NBP   : 54 : 
        public string nombre_de_Pays
        {
            get { return this.nbp; }
            set { this.nbp = value; }
        }


        private string fus;//FUS    )
        public string Fuseaux_horaires
        {
            get { return this.fus; }
            set { this.fus = value; }
        }


        private string pnb;//PNB  
        public string PNB_habitant
        {
            get { return this.pnb; }
            set { this.pnb = value; }
        }

        //constructeur
        public continent(string nom)
        {
            this.nomc = nom;
        }

        public continent()
        {
            // TODO: Complete member initialization
        }
       //list of countries of that continent 
          public List<country> listep = new List<country>();     
    }
  // class of countries 
  namespace WindowsFormsApplication1
{
    [Serializable]
    class country
    {

        //champs
        private string nom_p;
        public string Nom_pays
        {
            get { return this.nom_p; }
            set { this.nom_p = value; }
        }


        private string cap;//PCAP    
        public string Capitale
        {
            get { return this.cap; }
            set { this.cap = value; }
        }


        private string sup;// PSUP   
        public string Superficie
        {
            get { return this.sup; }
            set { this.sup = value; }
        }


        private string reg;// REG   
        public string Régime_politique
        {
            get { return this.reg; }
            set { this.reg = value; }
        }


        private string dev;//PDEV  nationale 
        public string Devise
        {
            get { return this.dev; }
            set { this.dev = value; }
        }


        private string hym;// PHYM 
        public string Hymne
        {
            get { return this.hym; }
            set { this.hym = value; }
        }


        private string lg;// PLG
        public string Langue
        {
            get { return this.lg; }
            set { this.lg = value; }
        }


        private string mo;// PMO
        public string Monnaie
        {
            get { return this.mo; }
            set { this.mo = value; }
        }


        private string de;
        public string PDE 
        {
            get { return this.de; }
            set { this.de = value; }
        }


        //constructeur
        public country (string nom)
        {
            this.nom_p = nom;
        }

    }
}
   and the code in the form is
    //liste of contnents 
        List<continent> listec = new List<continent>();
  // i filled it from a file 
  //here the code of toolstrip that open the childform

  private void listeContinentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listecont flc = new listecont(); 
            flc.ShowDialog();
           flc.MdiParent = this;

        }

3 个答案:

答案 0 :(得分:1)

在子表单中,向Form构造函数添加一个重载,该重载将Form作为参数。然后,当您创建子表单时,可以传入当前(父)表单的实例,例如listecont flc = new listecont(this);,其中this是您父表单的引用。现在,您的子表单可以调用parentForm.Textbox.Text = "blablabal"或者您希望与之交互的对象。

答案 1 :(得分:1)

为什么不将构造函数添加到listecont类中List<continent>?然后,子表单将在构建时获得数据。

答案 2 :(得分:1)

在你的MDI孩子中

添加一个方法:

public void SetContinentData(List<continent> list)
{
    // add your DataSource to the grid
    // f.e.:
    dataGridView.DataSource = list;
}

并在您的Toolstrip处理程序中:

private void listeContinentToolStripMenuItem_Click(object sender, EventArgs e)
{
    listecont flc = new listecont();
    flc.SetContinentData(listec);
    flc.ShowDialog();
    flc.MdiParent = this;      
}
相关问题