打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C# 子窗体与父窗体之间几种传值的方式
userphoto

2017.02.28

关注

做了很多项目,很多项目都用到子父窗体之间的传值。。

父窗体传入子窗体都比较简单,而子窗体传入父窗体因为有很多不通道的需求,所以·搞起来有点头大。


先说父窗体传入子窗体:

将父窗体控件上的值传入子窗体的控件上:

Form1为父窗体

Form2为子窗体

Form1 单击按钮打开Form2,Form2关闭的时候把值传到Form1的控件上

代码:

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace WindowsFormsApplication3  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void button1_Click(object sender, EventArgs e)  
  21.         {  
  22.             Form2 f2 = new Form2(this.textBox1.Text);  
  23.             if (f2.ShowDialog() == DialogResult.OK)  
  24.             {  
  25.                 this.textBox1.Text = f2.TextBoxValue;  
  26.                 f2.Close();  
  27.             }  
  28.   
  29.         }  
  30.   
  31.     }  
  32. }  


Form2的代码:

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace WindowsFormsApplication3  
  12. {  
  13.     public partial class Form2 : Form  
  14.     {  
  15.         public Form2():this(null)  
  16.         {  
  17.               
  18.         }  
  19.   
  20.         public string TextBoxValue  
  21.         {  
  22.             get { return textBox1.Text; }  
  23.             set { textBox1.Text = value; }  
  24.         }  
  25.   
  26.         public Form2(string value) {  
  27.             InitializeComponent();  
  28.             TextBoxValue = value;  
  29.         }  
  30.   
  31.         private void button1_Click(object sender, EventArgs e)  
  32.         {  
  33.             this.DialogResult = DialogResult.OK;  
  34.         }  
  35.          
  36.     }  
  37. }  

这是传值方式是最基本,租常见的一种,还有一种方式,就是打开子窗体Form2后,在往Form2上的控件上输入值的时候,Form1上的控件上的值也跟着变化,

对于这种情况,问你就需要使用 事件委托来完成。

----------------------------------------------------------快乐的分割线-------------------------------------------------------------

Form1代码:

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace WindowsFormsApplication3  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.           
  20.         private void button2_Click(object sender, EventArgs e)  
  21.         {  
  22.             Form2 f2 = new Form2();  
  23.             f2.TextBoxChanged += new EventHandler(  
  24.                 (sender1, e1) =>  
  25.                 { textBox2.Text = f2.TextBoxValue; }  
  26.             );  
  27.             f2.FormClosed += new FormClosedEventHandler(  
  28.                 (sender2, e2) => { f2 = null; }  
  29.             );  
  30.             f2.Show(this);  
  31.         }  
  32.     }  
  33. }  


Form2代码:

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace WindowsFormsApplication3  
  12. {  
  13.     public partial class Form2 : Form  
  14.     {  
  15.         public Form2():this(null)  
  16.         {  
  17.               
  18.         }  
  19.   
  20.         public string TextBoxValue  
  21.         {  
  22.             get { return textBox1.Text; }  
  23.             set { textBox1.Text = value; }  
  24.         }  
  25.   
  26.         public event EventHandler TextBoxChanged;  
  27.   
  28.         public Form2(string value) {  
  29.             InitializeComponent();  
  30.             TextBoxValue = value;  
  31.         }  
  32.   
  33.         private void button1_Click(object sender, EventArgs e)  
  34.         {  
  35.             this.DialogResult = DialogResult.OK;  
  36.         }  
  37.   
  38.         private void textBox1_TextChanged(object sender, EventArgs e)  
  39.         {  
  40.             if (TextBoxChanged != null)  
  41.             {  
  42.                 TextBoxChanged(this, e);  
  43.             }  
  44.         }  
  45.     }  
  46. }  


这里需要使用 TextBox的TextChanged事件。



上面就是两种子窗体传入父窗体值的方法。



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在WinForms程序里实现窗体传值的最佳实践
C#使用Event在窗体之间传递消息和参数
如何在多线程中调用winform窗体控件
在VB.Net中创建使用控件数组
Visual?C#?windows窗体示例主题(一)(MSDN整理)
windows form (窗体) 之间传值小结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服