Mtellef Sandala
Mtellef Sandala

Reputation: 21

C# Communication between parent form and child form

My project has two classes. The first class has information about continents and it contains also a list of objects of countries (another class).

I also declared a list of continents that contains all the continents. I've succeeded in filling the list from a file, and succeeded to show them in a DataGridView in the same form. But the problem is that I didn't find a way to show them in a child form that contains a DataGridView.

So, how can I transfer the list of continents to the child form so that I can be able to show them in it?

I tried serialiization and deserialization, but it didn't work, I just see the name of members of continent class and nothing else.

Here are the two class and code of toolstrip that show the child form:

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

        }

Upvotes: 2

Views: 864

Answers (3)

stefankmitph
stefankmitph

Reputation: 3306

in your MDI child add a method:

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

and in your Toolstrip handler:

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

Upvotes: 1

dsolimano
dsolimano

Reputation: 8986

Why not just add a constructor to the listecont class that takes a List<continent>? Then, the child form will have the data when it's constructed.

Upvotes: 1

Jacob Roberts
Jacob Roberts

Reputation: 1815

In your child form, add an overload to the Form constructor that takes a Form as an argument. Then when you create your child form, you can pass in an instance of your current (parent) form like, listecont flc = new listecont(this); where this is a reference of your parent form. Now your child form can make calls to parentForm.Textbox.Text = "blablabal" or what ever object you want to interact with.

Upvotes: 1

Related Questions