C 中窗體間資料傳遞的兩種方法

2021-04-29 06:51:53 字數 1056 閱讀 6449

c#中有時需要在其他窗體上訪問主窗體中的成員資料或系統中一些基本配置資料要在不同的窗體中使用,就需要在不同窗體間傳遞相同資料。以下總結兩種窗體間資料傳遞的方法。

1,訪問主窗體中的成員資料的方法:

(1),建立乙個應用程式(已有主窗體form1);

(2),再新增乙個窗體(form2);

(3),在form1中新增乙個按鈕button1;

(4),在form2中也新增乙個按鈕button1;

(5),在form2中定義乙個form1的物件:

public form1 m_form1;

(6),用以下**改寫form2的建構函式form2:

public form2(form1 myform)

(7),在form1中定義我們要傳遞的成員資料:

public int a;

(8),在form1中按鈕button1的單擊事件中加入以下**:

private void button1_click(object sender, eventargs e)

(9),在form2中按鈕button1的單擊事件中加入以下**用於測試:

private void button1_click(object sender, eventargs e)

//屬性

public static int a

set}

(6),在form1中按鈕button1的單擊事件中加入以下**:

private void button1_click(object sender, eventargs e)

{//賦值為12

class1.a = 12;

form2 m_form = new form2(this);

m_form.show();

(7),在form2中按鈕button1的單擊事件中加入以下**用於測試:

private void button1_click(object sender, eventargs e)

{//顯示值,12

messagebox.show(class1.a.tostring());

C 中窗體的資料傳遞

用vs2008中進行窗體之間資料的傳遞 首先用form1體作為主窗體,當主窗體之間修改資料時,子窗體也可以和主窗體資料相同,使用建構函式進行資料傳遞 using system using system.collections.generic using system.componentmodel u...

C 中窗體之間的資料傳遞

傳參的方法,主要利用arraylist 型別引數為引用傳遞的特性.主窗體 public partial class frmmain form private void btnadd click object sender,eventargs e 子窗體 public partial class fr...

C 實現窗體全屏的兩種方法

方法一 不過此方法有時候會出現莫名的bug 程式啟動路徑,與生成程式的exe檔案在同一目錄下 public string exepath application.startuppath 定義窗體寬高 int screenwidth 0 int screenheight 0 screenwidth i...