C#表單間傳值的兩種方法

來源:互聯網
上載者:User

首先建立一個父表單form1,擁有一下控制項:textBox1,button1;

再建立一個子表單form2,擁有控制項:textBox2,button2;

(首先描述一下整個過程:當點擊form1的button1時,開啟form2視窗,然後在textBox2裡面輸入任意值,當點擊button2時,將textBox2的值傳給textBox1,即實現表單間傳值)

 

第一種方法:

在button1的單擊事件中寫入如下代碼:

form2 f2 = new form2();

f2.ShowDialog(this);

 

在button2的單擊事件中寫入如下代碼:

form1 f1 = (form1)this.owner;

f1.textBox1.text =this.textBox2.text;

運行即可實現傳值。

 

 

第二種方法:

在button1的單擊事件中寫入如下代碼:

form2 f2 = new form2(this);

f2.ShowDialog();

 

在表單form2的表單類中聲明變數,並改寫初始化建構函式如下:

form1 form1;

public form2(form1 f1)

{

InitializeComponent();

form1 = f1;

}

 

在button1的點擊事件中寫入如下代碼:

form1.textBox1.text = this.textBox2.text;

運行即可實現傳值。

 

註:textbox1的Modifiers(控制項的可見度層級,即修飾符)應為:public ,internal,protected中之一,不能為private(私人)。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.