假設有兩個 Form (Form1、Form2),兩個 Form 上面各拉一個 Button (Button1),然後我要讓 Form2的Button1.Text顯示Form1的string變數s的值,跨表單傳值範例如下:

Form1 code:

public partial class Form1 : Form
{
    public string s = "123456";
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.f1 = this;
        f2.ShowDialog();
    }
}


Form2 code:

public partial class Form2 : Form
{
    public Form1 f1 = null;
    
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        button1.Text = f1.s;
    }
}

arrow
arrow
    全站熱搜

    浮雲 發表在 痞客邦 留言(0) 人氣()