c# winForm父子窗口信息交互方式介紹

1,從父窗體向子窗體傳值

// 從父窗體向子窗體傳值
private void btnParent_click(object sender, EventArgs e){
	//方法一 通過重寫構造函數傳值 重寫子窗體的構造函數
	// FrmChild chidl = new FrmChild(txtParent.Text);
	// child.ShowDialog();

	// 方法二 通過公共變量傳值,如果這個變量是在父窗體中聲明的,那麼需要時靜態變量
	// FrmChild chidl = new FrmChild();
	// strValue = textParent.Text;
	// clhild.ShowDialog();
	
	// 方法三 通過在子窗體中聲明公共變量
	FrmChild child = new FrmChild();
	child.strValue = txtParent.Text;
	child.ShowDialog();
}

2,從子窗體向父窗體傳值

public FrmChild(string textValue){
	InitializeComponment();
	txtChild.Text = textValue;
}
private void btnChild_Click(object sender, EventArgs e){
	// 方法一 將父窗體設置爲當前子窗體的擁有者
	// FrmParent parent = (FrmParent)this.Owner;
	//parent.Controls["txtParent"].Text = textChild.Text;
	
	//方法二 創建一個賦值的方法
	// FrmParent parent = (FrmParent)this.Owner;
	// parent.SetValue(txtChild.Text);

	// 方法三 
	FrmParent parent = (FrmParent)this.Owner;
	parent.SetTextValue = txtChild.Text;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章