我在这个表单上有一个名为“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上的提交按钮的click事件中,我使用此代码将控件数据存储到名为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 :(得分:4)
我建议您创建一个名为“模型”的类,它将包含您孩子的数据(如姓名,血型等)。
此模型类将成为所有数据的“主页”,您可以将其传递给任何其他需要它的表单或类 - 所以,现在您只传递1个参数。
如果有多个子节点,可以将ChildModels放入集合/数组/列表/或ObservableCollection(用于数据绑定)。然后你可以遍历列表中的每个孩子来操纵它们。
如果要将其转到下一步,请在模型上实现System.ComponentModel.INotifyPropertyChanged,并使用数据绑定在表单控件中显示数据,而不是手动将数据移入和移出控件
有关详细信息,请搜索“模型视图控制器”模式。
答案 1 :(得分: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; }
}
}
答案 2 :(得分:1)
我不确定你想要什么,但你肯定可以在Childrendata类中使用这样的构造函数:
public class Childrendata
{
public Childrendata(Children child)
{
childname = child.Name;
//etc...