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
【说明】转载请标明出处,谢谢
反馈文章质量,你可以通过快速通道评论:
查看全文
相关阅读:
阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (四) 自动化部署
阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (三) 服务观测
阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (二) 部署微服务程序
阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (一) 部署 Nacos
C++知识点
libmkl 学习笔记
基于tesseract-OCR进行中文识别
poco编译与运行
Linux下的I/O复用与epoll详解(转载)
高并发网络编程之epoll详解(转载)
原文地址:https://www.cnblogs.com/virusswb/p/1131276.html
最新文章
最短路径算法——Dijkstra算法
静态链接与动态链接库的查找顺序
关于delete的一个小问题
ubuntu16.04源码编译安装Poco1.7.8
sql去重 删除id比较的的
Flask Web学习笔记(六)
Flask Web学习笔记(五)
Flask Web学习笔记(四)
Flask Web学习笔记(三)
Flask Web学习笔记(二)
热门文章
Flask Web学习笔记(一)
记客户端出现Connect reset问题排查。
maven的setting配置远程仓库
windows运行Tomcat配置自定义的jdk环境运行。
仿谷歌验证 (Google Authenticator) 的一种 Java 实现
k8s 部署 (二) EMQ X 集群
k8s 部署 (一) SkyWalking 集群
阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (五) Kubernetes TCP Ingress
Jenkins 之权限 (Role-based Authorization Strategy)
Jenkins 之使用自定义主题
Copyright © 2011-2022 走看看