c# - 如何将大量参数传递给构造函数?

标签 c# .net winforms

我有一个名为“add.cs”的主窗体包含用户信息(它的名字,年龄......等)在这个窗体上我拖了一个按钮来添加用户的 child 信息(他们的名字、血型、出生日期等)的点击事件打开了一个表单“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 ");
                    }

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

最佳答案

我建议您创建一个名为“模型”的类,其中将包含您 child 的数据(如姓名、血型等)。

这个模型类将成为所有数据的“家”,您可以将它传递给任何其他需要它的表单或类——因此,现在您只需传递 1 个参数。

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

如果您想下一步,在您的模型上实现 System.ComponentModel.INotifyPropertyChanged,并使用数据绑定(bind)使数据在您的表单控件中可见,而不是手动将数据移入和移出控件.

有关更多信息,请搜索 Model View Controller 模式。

关于c# - 如何将大量参数传递给构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16385318/

相关文章:

c# - 从 HTML <figure> 和 <figcaption> 到 Microsoft Word

c# - 如何在 C# 中的 Winform 中嵌入 R 图?

c# - 如何在 C# WinForm 中删除/删除/撤消 DrawString

visual-studio - Visual Studio 2019 : How to edit menu

c# - 单击中键可滚动

c# - IEnumerator.Current 和 IEnumerator<T>.Current 的不同异常抛出行为

c# - 条件格式 excel epplus if (cell.value<0) 填红色

c# - 为什么 0L 在转换为对象时不等于 0?

javascript - 有什么方法可以覆盖我网站的 'CSS Theme' 吗?

c# - 以编程方式创建 Acrobat 的多个实例