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
【说明】转载请标明出处,谢谢
反馈文章质量,你可以通过快速通道评论:
查看全文
相关阅读:
拷贝本地文件到docker容器
python3 使用pip安装(命令行中)失败或 “not a supported wheel” 解决方案!
Scrapy框架爬虫
2019227单词记录
开班第一碗
函数进阶
Oracle数组
oracle merge into使用
oracle授权替代database link 速度加快
ora01031:权限不足
原文地址:https://www.cnblogs.com/virusswb/p/1131276.html
最新文章
C#中的委托和事件(续)
oracle函数返回表的写法
Oracle replace 函数用法
Android学习笔记(一)Windows XP 下Android开发环境搭建
sqlserver2008连接接口及版本的问题
SQL 维护计划来进行数据库备份和删除过期备份
MVC报错,写入字典的类型为****,但字典需要的类型为****
SQL 创建作业
ApiCloud套壳App,如何实现在断网的情况下提示并退出App
C# 使用Session解决前后台分离验证码问题
热门文章
ORM框架基本认识
c# 使用WebSocket来实现实时通讯
open the blogs
接口自动化集成到jenkins(Java+testng+maven+git)
联合查询时的NULL值转换问题的解决
样式和模板的学习笔记
集合各个实现类的底层实现原理 原文地址:https://blog.csdn.net/qq_25868207/article/details/55259978
快速查看linux命令的用法TLDR
工具链接
git中利用rebase来压缩多次提交 原文:https://blog.csdn.net/itfootball/article/details/44154121
Copyright © 2011-2022 走看看