zoukankan
html css js c++ java
winform+c#之窗体之间的传值 Virus
窗体传值可以分为两类。
1、主窗体往子窗体传值
有两种方法,一种是在子窗体提供重载构造函数,利用重载构造函数传递值,适用于传值数量比较少;第二种是,在子窗体中定义一个主窗体对象,然后就可以接收到主窗体的属性值了,适用于传值数量大。
主窗体代码如下:
public
partial
class
frmParent : Form
{
private
string
strValueA
=
""
;
public
string
StrValueA
{
get
{
return
this
.strValueA;
}
set
{
this
.strValueA
=
value; }
}
public
frmParent()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
this
.strValueA
=
textBox1.Text;
frmChild frmchild
=
new
frmChild();
frmchild.Owner
=
this
;
frmchild.ShowDialog();
frmchild.Dispose();
}
private
void
button2_Click(
object
sender, EventArgs e)
{
frmChild frmchild
=
new
frmChild(
this
.textBox1.Text);
string
returnValue
=
""
;
if
(frmchild.ShowDialog()
==
DialogResult.OK)
{
returnValue
=
frmchild.Str;
this
.textBox1.Text
=
returnValue;
}
}
}
子窗体代码如下:
public
partial
class
frmChild : Form
{
private
string
str;
public
string
Str
{
get
{
return
this
.str; }
set
{
this
.str
=
value; }
}
private
frmParent frmparent;
public
frmChild()
{
InitializeComponent();
}
public
frmChild(
string
str)
{
this
.str
=
str;
InitializeComponent();
this
.textBox1.Text
=
str;
}
private
void
frmChild_Load(
object
sender, EventArgs e)
{
frmparent
=
(frmParent)
this
.Owner;
//
this.textBox1.Text = frmparent.StrValueA;
}
private
void
button1_Click(
object
sender, EventArgs e)
{
//
frmparent = (frmParent)this.Owner;
this
.Str
=
this
.textBox1.Text;
this
.DialogResult
=
DialogResult.OK;
this
.Close();
}
}
2、从子窗体返回值到主窗体中
利用了子窗体的属性保存子窗体的值,在主窗体中可以访问到子窗体的属性
主窗体代码如下:
public
partial
class
frmParent : Form
{
private
string
strValueA
=
""
;
public
string
StrValueA
{
get
{
return
this
.strValueA;
}
set
{
this
.strValueA
=
value; }
}
public
frmParent()
{
InitializeComponent();
}
private
void
button2_Click(
object
sender, EventArgs e)
{
frmChild frmchild
=
new
frmChild(
this
.textBox1.Text);
string
returnValue
=
""
;
if
(frmchild.ShowDialog()
==
DialogResult.OK)
{
returnValue
=
frmchild.Str;
this
.textBox1.Text
=
returnValue;
}
}
}
子窗体代码如下:
public
partial
class
frmChild : Form
{
private
string
str;
public
string
Str
{
get
{
return
this
.str; }
set
{
this
.str
=
value; }
}
private
frmParent frmparent;
public
frmChild()
{
InitializeComponent();
}
private
void
frmChild_Load(
object
sender, EventArgs e)
{
frmparent
=
(frmParent)
this
.Owner;
//
this.textBox1.Text = frmparent.StrValueA;
}
private
void
button1_Click(
object
sender, EventArgs e)
{
//
frmparent = (frmParent)this.Owner;
this
.Str
=
this
.textBox1.Text;
this
.DialogResult
=
DialogResult.OK;
this
.Close();
}
}
【Blog】
http://virusswb.cnblogs.com/
【MSN】
jorden008@hotmail.com
【说明】转载请标明出处,谢谢
反馈文章质量,你可以通过快速通道评论:
查看全文
相关阅读:
Winform,C#,listView判断鼠标点击是行还是listView的空白区
利用正则表达式除去html得到纯文本
c#中如何退出程序后自动重新启动程序
c# winform SaveFileDialog保存文件示例和一些属性注释
WebBrowser自动点击链接 广告自动点击 Ads Auto Click
陶哲轩实分析定理 11.4.3 $\max$与$\min$保持黎曼可积性
陶哲轩实分析 例 1.2.12 洛必达法则使用注意事项
陶哲轩实分析 习题 10.3.5
陶哲轩实分析引理 11.1.4
陶哲轩实分析定理 11.4.3 $\max$与$\min$保持黎曼可积性
原文地址:https://www.cnblogs.com/virusswb/p/1131276.html
最新文章
突然奇想
乱想跨域
乱想(一)
乱想(二)
(转)一点一点学ASP.NETHttpHandler
乱想生命周期
乱想HTTP请求常用对象
乱想(四)
乱想(三)
再谈”闭包“
热门文章
Oracle EBS:根据请求简称在数据库PLSQL中查找请求信息 carlo
PLSQL:MERGE的用法 carlo
Oracle EBS:打开工作日历查看 carlo
Oracle EBS:Package被锁,执行时卡住的解决办法 carlo
Oracle EBS:FNDLOAD用法实例 carlo
微软发布手机操作系统Windows Phone 7(组图)
c# winform 关于给静态全局变量赋值的问题
服务器无法通过系统非页面共享区来进行分配,因为共享区当前是空的解决办法.供参考.
WebBrowser控件禁用超链接转向、脚本错误提示、默认右键菜单和快捷键
winform,C#,打开文件对话框的使用
Copyright © 2011-2022 走看看