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;
}

原文:c# winForm父子窗口信息交互方式介绍_51CTO博客_c# 窗口

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值