C#父窗体和子窗体之间的通信

C#父窗体和子窗体之间的通信,c#,forms,datagridview,C#,Forms,Datagridview,我的项目有两个类。第一类包含有关大陆的信息,还包含国家的对象列表(另一类) 我还宣布了一份包含所有大陆的大陆清单。 我已经成功地从文件中填充了列表,并成功地以相同的形式在DataGridView中显示了它们。但问题是,我没有找到一种方法在包含DataGridView的子表单中显示它们 那么,如何将大陆列表转移到子表单中,以便能够在其中显示它们呢 我尝试了序列化和反序列化,但没有成功,我只看到了Continental类成员的名字,其他什么都没有 下面是显示子窗体的toolstrip的两个类和代码:

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

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

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

我尝试了序列化和反序列化,但没有成功,我只看到了Continental类成员的名字,其他什么都没有

下面是显示子窗体的toolstrip的两个类和代码:

// 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;

        }
//大陆第一类
命名空间Windows窗体应用程序1
{
[可序列化]
阶级大陆
{
//冠军
私有字符串nomc;
公共字符串Nomc
{
获取{返回this.nomc;}
}
私有字符串sup;//sup
公共字符串超文本
{
获取{返回this.sup;}
设置{this.sup=value;}
}
私有字符串pop;//pop
公共字符串人口
{
获取{返回this.pop;}
设置{this.pop=value;}
}
私有字符串dens;//dens:
公共字符串密度
{
获取{返回this.dens;}
设置{this.dens=value;}
}
私有字符串nbp;//nbp:54:
公共字符串nombre_de_Pays
{
获取{返回this.nbp;}
设置{this.nbp=value;}
}
私有字符串fus;//fus)
公共字符串Fuseaux_horaires
{
获取{返回this.fus;}
设置{this.fus=value;}
}
私有字符串pnb;//pnb
公共字符串PNB_居住者
{
获取{返回this.pnb;}
设置{this.pnb=value;}
}
//建设者
公共大陆(字符串名称)
{
this.nomc=nom;
}
公共大陆()
{
//TODO:完成成员初始化
}
//该大陆的国家名单
public List listp=新列表();
}
//国家类别
命名空间Windows窗体应用程序1
{
[可序列化]
阶级国家
{
//冠军
私有字符串名称;
公共字符串Nom_pays
{
获取{返回this.nom_p;}
设置{this.nom_p=value;}
}
私有字符串cap;//PCAP
公共字符串大写字母
{
获取{返回this.cap;}
设置{this.cap=value;}
}
私有字符串sup;//PSUP
公共字符串超文本
{
获取{返回this.sup;}
设置{this.sup=value;}
}
私有字符串reg;//reg
公共字符串Régime_politique
{
获取{返回this.reg;}
设置{this.reg=value;}
}
私有字符串dev;//PDEV nationale
公共字符串设计
{
获取{返回this.dev;}
设置{this.dev=value;}
}
私有字符串hym;//PHYM
公共弦赞美诗
{
获取{返回this.hym;}
设置{this.hym=value;}
}
私有字符串lg;//PLG
公共字符串语言
{
获取{返回this.lg;}
设置{this.lg=value;}
}
私有字符串mo;//PMO
公共字符串蒙奈
{
获取{返回this.mo;}
设置{this.mo=value;}
}
私有字符串de;
公共字符串PDE
{
获取{返回this.de;}
设置{this.de=value;}
}
//建设者
公共国家(字符串名称)
{
this.nom_p=nom;
}
}
}
表格中的代码是
//内容列表
List listec=新列表();
//我是从档案里填的
//这里是打开子窗体的toolstrip代码
私有void ListToolStripMenuItem\u单击(对象发送方,事件参数e)
{
listcont flc=新的listcont();
flc.ShowDialog();
flc.MdiParent=此;
}

在子窗体中,将重载添加到以窗体为参数的窗体构造函数中。然后,在创建子窗体时,可以传入当前(父)窗体的实例,例如,
listcont flc=new listcont(this);
其中
这是您的父窗体的引用。现在您的子窗体可以调用
parentForm.Textbox.Text=“blablabal”
或您想要与之交互的任何对象。

为什么不向接受
列表的
listcont
类添加构造函数呢?然后,子窗体在构造时将具有数据。

在MDI子窗体中添加一个方法:

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

不相关,但您可能想查看自动属性您可能不想使用MdiParent属性,因为在设置对话框时您已经退出了该对话框。我怀疑您想做的是flc.ShowDialog(此)谢谢你,你的方法很简单,也很有效,谢谢sir@MtellefSandala如果它帮助你,请考虑接受我的答案。
private void listeContinentToolStripMenuItem_Click(object sender, EventArgs e)
{
    listecont flc = new listecont();
    flc.SetContinentData(listec);
    flc.ShowDialog();
    flc.MdiParent = this;      
}