我有一个名为“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 ");
}
请告诉我确切的例子,因为我已经尝试了很多来解决这个问题但没有输出!请帮忙请请