当前位置: 首页 > news >正文

C# 窗体间传值总结

转自: http://www.cnblogs.com/step/archive/2011/07/02/2096523.html

C# 窗体间传值总结

1、父窗体传值给子窗体

在父窗体中写:
FormChild fc = new  FormChil();//新建一个子窗体
fc.ShowDialog(this);//以上两句即实现了子窗体和父窗体的联系
textBox2.Text = fc.Str1;//已实现联系,可以传值
2、子窗体传值给父窗体
有两种方法:
  (1) 在子窗体中写:
        FormParent fp = (FormParent)this.Owner;//实现联系
        textBox1.Text = fp.Str1;//已实现联系,可以传值
   (2)运用子窗体的构造函数将父窗体的值传入(代码如下),可传一两个父窗体的值,如果要传的值多可以用ArrayList,也可直接传窗体(示例代码直接传窗体)
总结:抓住窗体对象和要传具体值的联系
代码:
Form1.cs

      
using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TestTransValue { public partial class Form1 : Form { private string form1Text; public string Form1Text { get { return form1Text; } set { form1Text = value; } } public Form1() { InitializeComponent(); form1Text = " 这是来自Form1的Text " ; } private void Form1_Load( object sender, EventArgs e) { } private void button1_Click( object sender, EventArgs e) { Form2 f2 = new Form2(); f2.ShowDialog( this ); textBox1.Text = f2.Form2Text; // 以上三句即可实现从子窗体向父窗体的传值 } private void button2_Click( object sender, EventArgs e) { Form3 f3 = new Form3( this ); f3.ShowDialog( this ); textBox2.Text = f3.Form3Text; // 以上三句即可实现从子窗体向父窗体的传值 } } }

Form2.cs

      
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TestTransValue { public partial class Form2 : Form { private string form2Text = " 这是来自form2的Text " ; public string Form2Text { get { return form2Text; } set { form2Text = value; } } public Form2() { InitializeComponent(); } private void Form2_Load( object sender, EventArgs e) { } private void button1_Click( object sender, EventArgs e) { Form1 f1 = (Form1) this .Owner; textBox1.Text = f1.Form1Text; // 以上两句即可实现子窗体获取父窗体的值(需要在父窗体中声明) } } }

Form3.cs

      
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TestTransValue { public partial class Form3 : Form { private string form3Text = " 这是来自form3的Text " ; Form1 f1; public string Form3Text { get { return form3Text; } set { form3Text = value; } } public Form3(Form1 form1) { InitializeComponent(); f1 = form1; } private void Form3_Load( object sender, EventArgs e) { } private void button1_Click( object sender, EventArgs e) { textBox1.Text = f1.Form1Text; } } }

相关文章:

  • 诺基亚5800XM 承认质量缺陷
  • ASP.NET Web Application 中使用 Unity 依赖注入容器
  • ARX 多段线的合并
  • 参加ESB沙龙
  • 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
  • Unix哲学基础
  • C# EXCEL 导入导出类(OLEDB的方式)
  • 相声:我要谈恋爱
  • C# 操作EXCEL样式 示例 --生成EXCEL审计表
  • C# 操作EXCEL
  • 新浪评出2008年度IT博客
  • c# 隐藏已打开的窗口,打开登陆窗口
  • 十个常用的S60手机软件
  • VB 操作ACCESS 实例 练习题--ATM取款机 代码段
  • 十大经典Java手机游戏 Top Ten Best Java Mobile Games
  • SegmentFault for Android 3.0 发布
  • 分享一款快速APP功能测试工具
  • 2017-09-12 前端日报
  • 2017年终总结、随想
  • CSS3 变换
  • JDK9: 集成 Jshell 和 Maven 项目.
  • webpack4 一点通
  • 复杂数据处理
  • 突破自己的技术思维
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 项目实战-Api的解决方案
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • zabbix3.2监控linux磁盘IO
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • # Apache SeaTunnel 究竟是什么?
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (2)MFC+openGL单文档框架glFrame
  • (八)c52学习之旅-中断实验
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (南京观海微电子)——I3C协议介绍
  • (五)IO流之ByteArrayInput/OutputStream
  • (一)appium-desktop定位元素原理
  • (转载)Google Chrome调试JS
  • .Net 8.0 新的变化
  • .net core控制台应用程序初识
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .NET 中使用 TaskCompletionSource 作为线程同步互斥或异步操作的事件
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • .NET高级面试指南专题十一【 设计模式介绍,为什么要用设计模式】
  • .NET企业级应用架构设计系列之开场白
  • .NET设计模式(11):组合模式(Composite Pattern)
  • .pop ----remove 删除
  • @ModelAttribute注解使用
  • []新浪博客如何插入代码(其他博客应该也可以)
  • [120_移动开发Android]008_android开发之Pull操作xml文件
  • [20170705]lsnrctl status LISTENER_SCAN1
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution
  • [④ADRV902x]: Digital Filter Configuration(发射端)
  • [BZOJ] 2006: [NOI2010]超级钢琴