You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过按钮点击将frmPlaceOrder(Form1)的用户信息传递至Form2?

窗体间传递数据的实现方案

嘿,我来帮你搞定把frmPlaceOrder里的客户姓名、地址传递到Form2的问题!你已经定义了CustomerInformation和Address类,刚好可以用它们来打包数据,下面给你两种简单靠谱的实现方式:

方法一:通过Form2的构造函数传递(推荐)

这种方式最直接,把需要的数据在打开Form2的时候就传给它:

  1. 完善Form2的构造函数
    在Form2里添加一个接收CustomerInformation和Address对象的自定义构造函数,同时保存这些数据供后续使用:
namespace Lab1_OrderCake
{
    public partial class Form2 : Form
    {
        private CustomerInformation _currentCustomer;
        private Address _currentAddress;

        // 自定义构造函数,接收客户和地址数据
        public Form2(CustomerInformation customer, Address address)
        {
            InitializeComponent();
            _currentCustomer = customer;
            _currentAddress = address;

            // 这里就可以用数据做你需要的操作了,比如显示在控件上
            // 假设你的CustomerInformation有FirstName、LastName属性
            lblCustomerName.Text = $"{_currentCustomer.FirstName} {_currentCustomer.LastName}";
            // 假设Address有Street、City、PostalCode属性,拼接完整地址
            lblCustomerAddress.Text = $"{_currentAddress.Street}\n{_currentAddress.City} {_currentAddress.PostalCode}";
        }
    }
}
  1. 在frmPlaceOrder里传递数据
    注意!不要在frmPlaceOrder的构造函数里初始化customer和address(那时候文本框是空的,拿不到用户输入的内容),要在触发跳转的事件里(比如点击“提交订单”按钮)更新数据,再打开Form2:
private void btnSubmitOrder_Click(object sender, EventArgs e)
{
    // 先获取用户输入的最新数据,实例化对象
    var customer = new CustomerInformation(txtFName.Text, txtLName.Text);
    var address = new Address(txtAddress.Text, txtCity.Text, txtPC.Text /* 补全你的其他参数 */);

    // 打开Form2并把数据传进去
    Form2 orderDetailsForm = new Form2(customer, address);
    orderDetailsForm.Show();

    // 如果需要关闭当前窗体,可以用 this.Hide(); 或者 this.Close();
}

方法二:通过Form2的公共属性传递

如果需要先创建Form2实例,再设置数据,这种方式更灵活:

  1. 在Form2里定义公共属性
    给Form2添加公共属性,用来接收外部传入的数据:
namespace Lab1_OrderCake
{
    public partial class Form2 : Form
    {
        // 定义公共属性
        public CustomerInformation Customer { get; set; }
        public Address CustomerAddress { get; set; }

        public Form2()
        {
            InitializeComponent();
        }

        // 可以在Form2加载完成后使用数据
        private void Form2_Load(object sender, EventArgs e)
        {
            if (Customer != null && CustomerAddress != null)
            {
                // 这里做数据展示或其他操作
                lblCustomerName.Text = $"{Customer.FirstName} {Customer.LastName}";
                lblCustomerAddress.Text = $"{CustomerAddress.Street}, {CustomerAddress.City}";
            }
        }
    }
}
  1. 在frmPlaceOrder里赋值并打开Form2
    同样在按钮事件里获取用户输入,然后给Form2的属性赋值:
private void btnSubmitOrder_Click(object sender, EventArgs e)
{
    var customer = new CustomerInformation(txtFName.Text, txtLName.Text);
    var address = new Address(txtAddress.Text, txtCity.Text, txtPC.Text /* 补全参数 */);

    Form2 orderDetailsForm = new Form2();
    // 给Form2的属性赋值
    orderDetailsForm.Customer = customer;
    orderDetailsForm.CustomerAddress = address;
    orderDetailsForm.Show();
}

重要提醒

  • 确保CustomerInformation和Address类的属性是公共可访问的(比如用public修饰),这样Form2才能读取里面的姓名、地址数据。
  • 一定要在用户输入完成后再实例化customer和address对象,不然拿到的会是空值哦!

内容的提问来源于stack exchange,提问作者Atheya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:22:50