如何通过单击按钮将多个文本框的数据从一种形式传递到另一种形式? [英] How to pass data of multiple text boxes from one form to another with button clicking?

查看:25
本文介绍了如何通过单击按钮将多个文本框的数据从一种形式传递到另一种形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过单击按钮将多个文本框的数据从 Form1 传递到 Form2.我尝试了以下方法,但它不起作用.

I'm not able to pass data of multiple text boxes from Form1 to Form2 through a button click. I have tried the following way, but it is not working.

我做错了吗?如果是这样,我该怎么做?

Did I do it wrong? And if so, how can I do it?

Form1 代码:

public partial class Form1: Form {
    Form2 frm2;

    public Form1() {
        InitializeComponent();
    }

    private void btnInvoice_Click_1(object sender, EventArgs e) {
        this.Hide();

        if(frm2==null)
            frm2=new Form2();

        frm2.ValueFromForm1(txtFirstName.Text);
        frm2.ValueFromForm1(txtLastName.Text);
        frm2.ValueFromForm1(txtCellNo.Text);
        frm2.ValueFromForm1(txtDate.Text);
        frm2.ValueFromForm1(txtDueDate.Text);

        frm2.Show();
    }
}

Form2 代码:

public partial class Form2: Form {
    public Form2() {
        InitializeComponent();
    }

    public void ValueFromForm1(string value) {
        txtFirstName.Text=value;
        txtLastName.Text=value;
        txtCellNo.Text=value;
        txtMaskDueDate.Text=value;
        txtMaskDate.Text=value;
    }
}

推荐答案

您正在为所有文本框分配相同的值.结果将是您分配的最后一个值(即 txtDueDate 文本).创建不同的方法来为每个文本框分配值,或将值作为数组或自定义对象传递:

You are assigning same value to all textboxes. And result will be last value you are assigning (which is txtDueDate text). Create different methods to assign values for each of textboxes, or pass values as array, or as a custom object:

public class Invoice
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public string CellNo { get; set; }
   // etc
}

在 Form1 上

private void btnInvoice_Click_1(object sender, EventArgs e)
{
    this.Hide();
    if (frm2 == null)
        frm2 = new Form2();

    Invoice invoice = new Invoice();
    invoice.FirstName = txtFirstName.Text;
    invoice.LastName = txtLastName.Text;
    invoice.CellNo = txtCellNo.Text;
    // etc
    frm2.Invoice = invoice;
    frm2.Show();
}

在 Form2 上

public Invoice Invoice
{
   set
   {
        txtFirstName.Text = value.FirstName;
        txtLastName.Text = value.LastName;
        txtCellNo.Text = value.CellNo;
        // etc
   }
}

这篇关于如何通过单击按钮将多个文本框的数据从一种形式传递到另一种形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆