通过按钮单击将form1中的值从form1传递到form2

时间:2018-04-06 08:32:06

标签: c# forms visual-studio multiple-forms

frmPlaceOrder是我的form1。我需要将此表单中的firstName,lastname和Address传递给第二个将执行其他功能的表单。我不知道该怎么做。

JdbcBatchItemWriter<List<String>>

第二种形式;在填写第一个之后,需要从form1获取值并将其与第二个表单中的其他值(frmCakeOrder值)一起显示。当我点击它时,需要在View和Order事件中看到它。

这是第二种形式:

namespace Lab1_OrderCake
{
public partial class frmPlaceOrder : Form
{
    public static CustomerInformation customer;
    public static Address address;

    public frmPlaceOrder()
    {
        InitializeComponent();
        customer = new CustomerInformation(txtFName.Text, txtLName.Text);
        address = new Address(txtAddress.Text, txtCity.Text, txtPC.Text, txtProvince.Text);

    }

    private void btnPlaceOrder_Click(object sender, EventArgs e)
    {


        DialogResult dlgMsg;
        if (txtFName.Text == "")
        {
            MessageBox.Show("Please enter first name", "Data Missing");
            txtFName.Focus();
            return;
        }
        if (txtLName.Text == "")
        {
            MessageBox.Show("Please enter Last name", "Data Missing");
            txtLName.Focus();
            return;
        }
        else
        {
            frmCakeOrder  newCust = new frmCakeOrder();
            this.Hide();
            newCust.ShowDialog();
            this.Close();

        }

    }
  }
}

2 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点。试试这种方式。

private void btnPlaceOrder_Click(object sender, EventArgs e) {
   string fname = textBox1.Text;
   frmCakeOrder frm = new frmCakeOrder(textBox1.Text);
   frm.Show();
} 

在frmCakeOrder中,

public frmCakeOrder(string fname) {
   InitializeComponent(); 
   textBox1.Text = fname; 
}

答案 1 :(得分:0)

您可以在构造函数中传递数据:

public class Form1: from{
    //constructor
    public void Form1(){
    }

    public void button_click(){
        //Get the data
        var firstName = textFirstName.text;
        var secondName= textSecondName.text;
        var address= textAddress.text;
        //Pass the data on the constructor of Form2
        Form2 f2 = new Form2(firstName,secondName, address);
        f2.show();
    }
}

public class Form2: Form{
    //constructor with data or parameters
    public void Form2(string firstName, string lastName, string Address){
         //do dosomething with the data
         txtFirstName.text = firstName;
    }
}

*对不起,如果它有sintaxys错误....但这就是想法。

相关问题