Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a main form named "add.cs"containing user information (its name , age ....etc.) on this form i have drag a button to add the children information(its name , blood group , date of birth..etc)whose click event opens a form "addchild.cs" on which i have dragged two button one for stroring data and another to add controls at runtime. To add controls at runtime i have used this coding
C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                childrenCount++;

                Label lblchild = new Label();
                lblchild.BorderStyle = BorderStyle.Fixed3D;
                lblchild.Location = new Point(20, (childrenCount - 1) * 50);
                lblchild.Size = new System.Drawing.Size(50, 20);
                lblchild.Text = "Child  " + (childrenCount - 1);
                TextBox tName = new TextBox(); //TextBox for the name
                tName.BorderStyle = BorderStyle.Fixed3D;
                tName.Location = new Point(120, (childrenCount - 1) * 50);
                tName.Size = new System.Drawing.Size(135, 60);
                tName.Text = "";
                DateTimePicker calendar = new DateTimePicker();
                //MonthCalendar calendar = new MonthCalendar(); //Calendar to choose the birth date
                calendar.Location = new Point(310, (childrenCount - 1) * 50);
                ComboBox bloodGroup = new ComboBox(); //ComboBox for the blood group
                bloodGroup.Location = new Point(650, (childrenCount - 1) * 50);
                bloodGroup.Size = new System.Drawing.Size(135, 60);
                for (Enum.Blood_Group l = Enum.Blood_Group.O_negative; l <= Enum.Blood_Group.AB_positive; l++)
                {
                    bloodGroup.Items.Add(l);
                }

                this.panel1.Controls.Add(lblchild);
                this.panel1.Controls.Add(tName);
                this.panel1.Controls.Add(calendar);
                this.panel1.Controls.Add(bloodGroup);


            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

Now I have created a separate external class named "Childrendata" for storing the value of controls added on " addchild.cs". These class contains all the data that i want to store.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jain_milan
{
    class Childrendata
    {
        string childname, childblood, childbirth;


        public string Childbirth
        {
            get { return childbirth; }
            set { childbirth = value; }
        }

        public string Childblood
        {
            get { return childblood; }
            set { childblood = value; }
        }

        public string Childname
        {
            get { return childname; }
            set { childname = value; }
        }

    }
} 

then on the click event of the submission button on addchild.cs i have used this code to store the controls data to the variables of external class called Childdata.cs.
C#
private void submitbtn_Click(object sender, EventArgs e)
      {
          //Checks the values
          try
          {
              string message = "";
              foreach (Control c in this.Controls)
              { //loop throught the elements of the form
                  if (c.GetType() == typeof(Panel))
                  { //check if the control is a Panel
                      //Get the values from the input fields and add it to the message string
                      Panel panel1 = (Panel)c;
                      Label lblchild = (Label)(c.Controls[0]);
                      TextBox tName = (TextBox)(c.Controls[1]);
                      DateTimePicker calendar = (DateTimePicker)(c.Controls[2]);
                      ComboBox bloodGroup = (ComboBox)(c.Controls[3]);

                      //add to class
                      Childrendata childdata=new Childrendata();
                      childdata.Childname = tName.Text;
                      childdata.Childblood= bloodGroup.Text;
                      childdata.Childbirth=calendar.Text;


                  }
              }
          }

          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
       }

Now how can i made the constructor taking array or any other thing as parameter and that store all the values and i can call this constructor to my main page named add.cs .So on click event of a button on add.cs i may store these values into database by using coding like this.
C#
Childrendata cd = new Childremdata();
Children child = new Children();

                    child.Namechild = cd.Childname;
                    child.Bloodchild = cd.Childblood;
                    child.Dobchild = cd.Childbirth;
                    if (new InsertAction().Insertchildren(child))
                    {
                        MessageBox.Show(" Children Insertion Happen ");
                    }
                    else
                    {
                        MessageBox.Show(" Children Insertion does not Happen ");
                    }

Please tell me the exact example because i have tried a-lot to solve this but no output!! Please help please please please
Posted
Updated 5-May-13 6:40am
v4
Comments
[no name] 5-May-13 11:47am    
I have read this several times and I sure can't figure out what it is that you are asking. "this class", "that class", "main page", do not really mean anything especially in this context. Sounds almost like you do not know how to write a class constructor but it's really unclear what it is that you are asking.
Member 8783598 5-May-13 12:25pm    
@ThePhantomUpvoter i have done some eition now please read it now may be you able understand my question
[no name] 5-May-13 14:18pm    
So create a constructor for your Childrendata class that takes whatever data you want it to have. I am not seeing what the problem is. Any decent book on programming would show you how to create a class constructor in the first 5 chapters.

All you need is to use List<t>[^] generic class, which represents a strongly typed list of objects that can be accessed by index and provides methods to search, sort, and manipulate lists.

But before you start programming, you need to correct your class to store correct types of values:
C#
class Child
{
    private string sname = string.Empty;
    private string sblood = string.Empty;
    private DateTime dbirth = DateTime.MinValue; //date of birth must be datetime!

    //constructor with few input parameters
    public Child(string childname, string childblood, DateTime  childdbirth )
    {
        sname = childname;
        sblood = childblood;
        dbirth = childdbirth;
    }

    public DateTime  Childbirth
    {
        get { return dbirth; }
        set { dbirth = value; }
    }

    public string Childblood
    {
        get { return sblood; }
        set { sblood = value; }
    }

    public string Childname
    {
        get { return sname; }
        set { sname = value; }
    }
}


Usage:
C#
List<child> oLstChilds = new List<child>();
Child oChild1 = new Child("Adam", "A Rh+", new DateTime(2013, 1, 5));
oLstChilds.Add(oChild1);
Child oChild2 = new Child("Julia", "B Rh+", new DateTime(2013, 2, 7));
oLstChilds.Add(oChild2);
Child oChild3 = new Child("Carlo", "0 Rh-", new DateTime(2013, 3, 9));
oLstChilds.Add(oChild3);
Child oChild4 = new Child("Maciej", "0 Rh+", new DateTime(2013, 4, 11));
oLstChilds.Add(oChild4);

MessageBox.Show (string.Format("Count of Childs: {0}", oLstChilds.Count));</child></child>
 
Share this answer
 
If you need to pass a large number of parameters to a constructor then you have many different options. You've already found one: an array (of objects). Another approach would be using, for instance a Dictionary<string,> (see MSDN[^]) whose keys are the parameter names while values are the parameters themselves.
 
Share this answer
 
Comments
Member 8783598 5-May-13 13:04pm    
okz than after creating instance of dictionary and passing values to it how can i pass this to constructor and call constructor to add.cs
CPallini 5-May-13 13:33pm    
(1) Make a constructor of the class accepting such a Dictionary.
(2) Create and populate the Dictionary.
(3) Pass it to the constructor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900