【问题标题】:How to transfer data from one form to another in winforms c#?如何在winforms c#中将数据从一种形式传输到另一种形式?
【发布时间】:2013-05-03 00:20:33
【问题描述】:
  1. 我想在windows应用程序中创建5-6个表单来插入数据。
  2. 每个表单至少包含 15-20 个控件。所有表格都属于 不同的表。但有些是一样的。
  3. 我必须在每个表单上创建存储的“下一步”命名按钮,这样 当我单击下一个按钮时,该按钮上填写的所有信息都会存储在某个位置
    并以这种方式将信息存储在提交按钮所在的最后一个按钮上 拖动,单击该提交按钮,所有数据都保存到数据库中。
  4. 请告诉我如何存储以前表单中插入的数据并在提交按钮的点击事件中调用它。

现在我在同一页面上拥有所有控件,并且我已使用这些代码进行插入。

 private void submit_addbtn_Click(object sender, EventArgs e)
        {
            try
            {
                //personal data insert
                Personal per = new Personal();
                per.Name = nametxt.Text;
                per.FatherName = f_nametxt.Text;
                per.MotherName = m_nametxt.Text;
                per.Gotra = gotra_txt.Text;
                per.Panth = panthcb.Text;
                per.FamilyHead = fhntext.Text;
                per.Educationlvl = edulvlcb.Text;
                per.Education = educb.Text;
                per.Blood = bloodcb.Text;
                per.Gender = genderlist.Text;
                per.Marrital = MarritalStatus;
                per.DateOfBirth = dobdtp.Text;
                if (new InsertAction().Insertpersonal(per))
                {
                    MessageBox.Show("Personal Insertion Happen ");
                }
                else
                {
                    MessageBox.Show(" Personal Insertion does not Happen ");
                }

                // spouse data insert
                Spouse sps = new Spouse();
                sps.Spousename = s_nametxt.Text;
                sps.Spouseeducationlvl = s_edulvlcb.Text;
                sps.Spouseeducation = s_educb.Text;
                sps.Spouseblood = s_bgcb.Text;
                sps.Spousedob = s_dobdtp.Text;
                if (new InsertAction().Insertspouse(sps))
                {
                    MessageBox.Show(" Spouse Insertion Happen ");
                }
                else
                {
                    MessageBox.Show(" Spouse Insertion does not Happen ");
                }

                // Resident data insert
                Ressident resi = new Ressident();
                resi.RessiHnumber = ressi_numtxt.Text;
                resi.RessihCmplx = ressi_complextxt.Text;
                resi.RessiStrt = ressi_streettxt.Text;
                resi.RessiLandmrk = ressi_landtxt.Text;
                resi.RessiArea = ressi_areatxt.Text;
                resi.RessiCity = ressi_citytxt.Text;
                resi.RessiPhone = Convert.ToInt64(ressi_phnotxt.Text);
                resi.RessiMobile = Convert.ToInt64(mobi_notxt.Text);
                if (new InsertAction().Insertressident(resi))
                {
                    MessageBox.Show(" Ressident Insertion Happen ");
                }
                else
                {
                    MessageBox.Show(" Ressident Insertion does not Happen ");
                }
                //occupation data insert
                Occupation ocp = new Occupation();
                ocp.Occuptype = occup_typetxt.Text;
                ocp.Occupadd = office_addresstxt.Text;
                ocp.Occupnature = occup_naturecb.Text;
                ocp.Occupphone = Convert.ToInt64(office_phno1txt.Text);
                ocp.Occupmobile = Convert.ToInt64(office_mobnotxt.Text);
                if (new InsertAction().Insertoccupation(ocp))
                {
                    MessageBox.Show(" Occupation Insertion Happen ");
                }
                else
                {
                    MessageBox.Show(" Occupation Insertion does not Happen ");
                }


            }

请帮助我。 谢谢。

【问题讨论】:

  • 您还可以创建 5-6 个与您的表单相关的类对象(每个表单一个类)和公共定义(set;get;)也与每个表单上的每个对象相关。在每个“下一个”之后" 点击,您可以将有关该表单的所有属性保留在其 classObject 中。希望对您有所帮助

标签: c# .net winforms sql-server-2008


【解决方案1】:

对于在两个表单之间发送值,您可以

1-> 发送第二种形式的构造函数中的值。您可以创建一个参数化构造函数并在将表单初始化为时发送值:

 Form1 obj = new Form1(Object);

2->您可以在第二个表格中引用您的第一个表格。

第二种形式,

public Form1 objForm1;

在第一种形式中,

Form2 objForm2=new Form2();
Form2.objForm1=this;

然后你可以使用Form2的objForm1来引用Form1的文本框或任何控件。

编辑:

考虑您要将所有值从Form1 发送到Form2

在您的第二个表单中,您必须有一个类型为 Form1 的变量来引用前一个表单。所以在第二种形式中,

public Form1 objForm1;

然后你需要将Form1的当前实例发送到Form2作为

Form2 objForm2=new Form2();
Form2.objForm1=this;

即您在 Form2 中创建的 objForm1 引用了 Form1 的这个实例。

现在在 Form2 中,您可以使用 Form1 的任何控件或变量作为,

Form1.TextBox1Form1.Variable

【讨论】:

  • 嘿,请准确告诉我,我应该在表格 1 上做什么,然后在第二个而不是第三个。请为每个表单发布至少 2-3 行代码,以便我更清楚......请
  • 我已经通过它bt这是我们必须只传递值bt在多个值的情况下该怎么办
  • Form2.objForm1=this;显示错误我已在类级别编写此代码
  • 表示Form2.objForm1=this;是一个字段,但用作类型和无效标记 '='
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多