2013-05-05 128 views
3

我有一个主窗体名为“add.cs”包含这个表单上的用户信息(它的名字,年龄等),我拖动一个按钮来添加用户的孩子信息(他们的名字,血液组,出生日期等),其单击事件打开一个表单“addchild.cs”,其中拖动了两个按钮之一来存储数据,另一个用于在运行时添加控件。在运行时添加控件我已经使用这个编码如何将大量参数传递给构造函数?

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); 
      } 

     } 

现在我已经创建了一个名为“Childrendata”用于存储的“addchild.cs”添加的控件值单独外部类。这些类包含我想要存储的所有数据。

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

    } 
} 

然后在提交按钮上addchild.cs单击事件我已经使用这个代码的控制数据存储到外部类的所谓Childdata.cs的变量。

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); 
      } 
     } 

现在,我怎么做的构造以阵列或任何其他东西作为参数和存储的所有值,我可以调用此构造到我的主命名页面add.cs。所以上的按钮的单击事件在add.cs我可以通过使用这样的编码将这些值存储到数据库中。

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 "); 
        } 

请告诉我确切的例子,因为我已经尝试了很多来解决这个问题,但没有输出! 请帮助请请请

+0

澄清你在问什么。你想能够使用类似列表或数组的东西来实例化你的类? – Nomad101 2013-05-05 14:15:23

+0

没有其实我想存储在子窗体中的所有控件的值到父窗体 – VJain 2013-05-05 14:40:00

回答

1

我真的不知道你想要什么,但你肯定可以有构造函数Childrendata类是这样的:

public class Childrendata 
{ 
    public Childrendata(Children child) 
    { 
     childname = child.Name; 
     //etc... 
+0

不,我不想要这个。我实际上想在窗体类中创建一个构造函数,这样我就可以在其中存储所有控件的值,并在父窗体中调用它。 – VJain 2013-05-05 14:44:52

4

我建议你创建一个类,叫做一个“模型”,将包含你的孩子的数据(如姓名,血型等)。

此模型类将成为所有数据的“主”,您可以将它传递给任何需要它的窗体或类 - 所以,现在您只传递1个参数。

当您有多个孩子时,您可以将您的ChildModels放入一个集合/数组/列表/或ObservableCollection(用于数据绑定)。然后你可以遍历列表中的每个孩子来操纵它们。

如果您想要将其转换为下一步,请在您的模型上实现System.ComponentModel.INotifyPropertyChanged,并使用数据绑定使数据在窗体控件中可见,而不是通过控件将数据移入和移出控件手。

有关更多信息,请搜索模型视图控制器模式。

+0

请给我举一个例子,请举例说明我该如何做到这一点。 – VJain 2013-05-05 14:40:56

+0

我创建的名称为“Childdata”的类包含所有数据和模型类别之间的区别请告诉我 – VJain 2013-05-05 14:53:47

+0

您的ChildData类是一个体面的模型示例 - 您绝对是在正确的轨道上。重新阅读原文后,我认为你看起来不错。我建议读一下Databinding。数据绑定将使您免于编写将数据移入和移出窗体控件所涉及的繁琐代码。这里有一个与Winforms数据绑定的教程:http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial – JMarsch 2013-05-05 17:35:16

3

你可以对这个代码来看看:

Add.cs

public class Add 
    { 
     private List<Childrendata> children; //Create this List of Childrendata in Add.cs form 

     public void CreateChildControls() // This is on Button click to create another from with controls 
     { 
      children = new List<Childrendata>(); // Call Add.cs form and pass the List 
      AddChild obj = new AddChild(children); 
      obj.StoreData(); 
     } 

     public void StoreInDB() // This is on final Submit to DB 
     { 
      if (new InsertAction().Insertchildren(children)) // Pass the same List of Childrendata to Save in DB 
      { 
       MessageBox.Show(" Children Insertion Happen "); 
      } 
      else 
      { 
       MessageBox.Show(" Children Insertion does not Happen "); 
      } 
     } 
    } 

AddChild.cs

public class AddChild 
{ 
    private List<Childrendata> m_children; 
    public AddChild(List<Childrendata> children) 
    { 
     this.m_children = children; //Initialize the same List as sent by Mainform 
    } 

    public void AddControls() 
    { 

    } 

    public void StoreData() // This is on Submit button click in child form to add new child details 
    { 
     for (int i = 0; i < 10; i++) 
     { 
      // Filled with some dummy data 
      Childrendata childdata = new Childrendata(); 
      childdata.Childname = "skumar" + i; 
      childdata.Childblood = "B" + i; 
      childdata.Childbirth = "2013/10/"+ i; 

      m_children.Add(childdata); // Add to same List 
     } 
    } 
} 

Childrendata。cs

public 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; } 
     } 

    } 
+1

我提出这个想法是因为它开始遵循模型/视图/演示者模型我更喜欢。它允许一个人单独测试表单和数据。再加上这种方式,在检查输入的数据时,它给你更多的自由。可能需要更多时间来设置,这将为您节省时间。 – 2013-05-05 19:34:39

+0

谢谢先生,先生,请你告诉我,我可以在方法里面添加一个方法,就像你上面说的在按钮点击功能中创建一个函数 – VJain 2013-05-07 12:53:12

+1

@ user2214972:你不能在其他方法中创建方法。我的意思是说,创建一个单独的方法,并从您的按钮点击方法中调用它。 – 2013-05-07 12:57:35

相关问题