惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
T
Troy Hunt's Blog
N
News and Events Feed by Topic
雷峰网
雷峰网
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 三生石上(FineUI控件)
Schneier on Security
Schneier on Security
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 最新话题
V
V2EX
T
Threat Research - Cisco Blogs
人人都是产品经理
人人都是产品经理
C
Cisco Blogs
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
I
Intezer
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
月光博客
月光博客
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News | PayPal Newsroom
Cyberwarzone
Cyberwarzone
B
Blog
博客园 - 聂微东
P
Palo Alto Networks Blog
A
About on SuperTechFans
The Last Watchdog
The Last Watchdog
Scott Helme
Scott Helme
Google DeepMind News
Google DeepMind News
Webroot Blog
Webroot Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
O
OpenAI News
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
A
Arctic Wolf

博客园 - nestea

Method 'Post' of object 'IOWSPostData' failed(方法'POST'作用于对象'IOWSPostData'时失败) 用Win XP安装盘修复系统文件 手机呼叫流程 XML配置字符串中特殊字符的处理 - nestea - 博客园 关于BackgroundWorker的使用 分布式缓存系统Memcached简介与实践[转] web应用程序中使用MemcachedClient (转载) 一些很酷的.Net技巧(转载) sql server中的decimal或者numeric的精度问题 (转载) 使用BackgroundWorker组件进行异步操作编程(转载) 操作DataTable中动态的行,一点收集整理资料。 xml转换成DataTable - nestea - 博客园 预处理命令之条件编译(转) 数字的进制(2进制,8进制,16进制) 开放式并发冲突检测的四种方法 MDX 中的重要概念 (MDX) 实用工具-文件夹实时同步工具 (群集服务器拷贝) 用windows 2003建立VPN 连接(转载) - nestea Windows 2003下VPN服务器架设攻略 (转载) - nestea
C#中父窗口和子窗口之间实现控件互操作 (转载)
nestea · 2009-04-30 · via 博客园 - nestea

  很多人都苦恼于如何在子窗体中操作主窗体上的控件,或者在主窗体中操作子窗体上的控件。相比较而言,后面稍微简单一些,只要在主窗体中创建子窗体的时候,保留所创建子窗体对象即可。

  下面重点介绍前一种,目前常见的有两种方法,基本上大同小异:

  第一种,在主窗体类中定义一个静态成员,来保存当前主窗体对象,例如:

public static yourMainWindow pCurrentWin = null;

  然后在主窗体构造函数中,给静态成员初始化,如下:

pCurrentWin = this;

  那么在子窗体中调用父窗体,可以通过“主窗体类名. pCurrentWin”来操作当前的主窗体。

  第二种,是在子窗体中定义一个私有成员,来保存当前主窗体对象,例如:

private yourMainWindow pParentWin = null;

  然后在子窗体构造函数中,加一参数,如下:

public yourChildWindow( yourMainWindow WinMain )
{
 pParentWin = WinMain;
 //Other code
}

  在主窗体创建子窗体的时候,要把this作为参数来构造子窗体,这样在子窗体中调用父窗体,可以直接用“this.pParentWin”就可以了

  不过以上所作的,只是让你能够访问当前主窗体对象,那么如何操作控件,很多人直接修改控件的成员访问符,即把“private”改为“public”,我觉得这样破坏了本身类的封装,所以我比较喜欢的做法是增加公有属性或方法来供调用,例如:

public string ButtonText
{
 get{ return btn.Text;}
 set{ btn.Text = value;}
}

public void Button_Click()
{
 this.btnDConvert.PerformClick();//Execute button click
}