C#实现WinForm传值实例解析

开发 后端
C#实现WinForm传值是如何做到的呢?我们在处理WinForm传值的时候会用到什么方法呢?那么本文就向你介绍具体的C#实现WinForm传值的具体步骤和实例解析。

C#实现WinForm传值的问题经常会做为公司面试的题目,那么作为学习C#以及WinForm传值,我们需要掌握哪些方法和思路呢?下面我们就向你介绍详细的思路和实现的具体步骤,希望对你有所帮助。

C#实现WinForm传值的思路:

从Form1传递到Form2: 2个窗体即两个类,两个窗体间的数据传送,可以采用构造函数来实现。

从Form2返回到Form1,并传递数据:实例化Form2后,打f2用ShowDialog()方法,然后等待f2关闭时再回传数据到Form1。

C#实现WinForm传值步骤及代码:

1:新建两个窗口: Form1,Form2;

2:打开Form2,添加一个textBox:textBox1;添加一个Button:button1;然后添加一个构造函数:

  1. //定义一个变量,用来传值。  
  2. public string returnValue ;  
  3.  
  4. public Form2(string txtValue)  
  5. {  
  6.   InitializeComponent();  
  7.  
  8.   this.textBox1.Text = txtValue;  
  9. }  

然后在button1的单击事件中添加如下代码:

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.   returnValue = this.textBox1.Text;  
  4.   this.Close();  

3:Form1中添加一个textBox:textBox1;添加一个Button:button1;然后在button1的单击事件中添加如下代码:

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.   string txtValue = this.textBox1.Text;  
  4.   Form2 f2 = new Form2(txtValue);  
  5.   f2.ShowDialog();  
  6.   this.textBox1.Text = f2.returnValue;  

Form1 中 (父窗口:)

  1. public class Form1 : System.Windows.Forms.Form  
  2. {  
  3.  private System.Windows.Forms.Button btnOpen;  
  4.  public System.Windows.Forms.TextBox txtContent;   
  5. //注意是public  
  6.  
  7.   ........  
  8.  
  9.   ........  
  10.  
  11.  [STAThread]  
  12. static void Main()  
  13. {  
  14. Application.Run(new Form1());  
  15. }  
  16.  
  17.  private void btnOpen_Click(object sender, System.EventArgs e)  
  18.  {  
  19.   Form2 frm=new Form2(this);  
  20.   frm.ShowDialog();  
  21.  }  
  22.  
  23. }  

Form2中(子窗口)

  1. public class Form2 : System.Windows.Forms.Form  
  2. {  
  3.  private System.Windows.Forms.Button button1;  
  4.  private System.Windows.Forms.TextBox txtValue;  
  5.  
  6.  private Form _parentForm=null;  
  7.  
  8.   public Form2()  
  9.   {  
  10.   InitializeComponent();   
  11.   }  
  12.  
  13.  public Form2(Form parentForm)  
  14.  {  
  15. InitializeComponent();  
  16. this._parentForm =parentForm;  
  17.  }  
  18.  
  19.  ........  
  20.  
  21. ........  

更新父窗口中文本框中的值!

  1. private void button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.  ((Form1)_parentForm).txtContent.Text =this.txtValue .Text ;  
  4. }  

C#实现WinForm传值的内容和相关的知识就向你介绍到这里,希望对你了解和学习C#实现WinForm传值的问题有所帮助。

【编辑推荐】

  1. C# main函数应用实例详解
  2. 浅析C# Main参数输入问题
  3. 详解C# Main方法返回值
  4. 浅析C#窗体的设置及属性介绍
  5. 实现C#窗体间传值详解
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-08-31 17:16:12

C#实现接口

2009-09-07 03:44:50

C#窗体间传值

2009-09-01 13:59:01

C#操作Excel

2009-09-01 16:59:25

C#画直线

2009-08-24 16:37:41

C# Winform刷

2009-09-03 17:06:17

C#回车切换焦点

2009-09-03 17:23:45

C#发送邮件

2009-09-02 16:14:21

C#动态创建数组

2009-09-09 14:40:15

C# XML解析

2009-09-09 13:57:28

C# XML解析

2009-08-18 10:47:40

C#枚举类型

2009-08-17 15:48:47

C# WinForm进

2009-08-26 12:14:44

C#打印设置

2009-09-07 06:31:32

C#窗体移动

2009-08-31 18:17:32

C#接口编程

2009-08-19 16:09:15

C#操作Access

2009-08-27 17:40:21

C#接口的作用

2009-08-28 12:31:06

C#静态方法

2009-09-03 12:52:50

C#打开记事本

2009-08-20 10:24:52

C#开发WinForm
点赞
收藏

51CTO技术栈公众号