2013-05-05 147 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

相關問題