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;然后添加一个构造函数:

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

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

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

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

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

Form1 中 (父窗口:)

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

Form2中(子窗口)

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

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

private void button1_Click(object sender, System.EventArgs e)  
{  
 ((Form1)_parentForm).txtContent.Text =this.txtValue .Text ;  
}  
  • 1.
  • 2.
  • 3.
  • 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-08-24 16:37:41

C# Winform刷

2009-09-01 16:59:25

C#画直线

2009-09-09 14:40:15

C# XML解析

2009-09-03 17:23:45

C#发送邮件

2009-09-02 16:14:21

C#动态创建数组

2009-09-03 17:06:17

C#回车切换焦点

2024-09-26 00:00:00

Thread间传值C#

2009-08-18 10:47:40

C#枚举类型

2009-09-09 13:57:28

C# XML解析

2009-09-07 06:31:32

C#窗体移动

2009-08-19 16:09:15

C#操作Access

2009-08-31 18:17:32

C#接口编程

2009-08-26 12:14:44

C#打印设置

2009-08-17 15:48:47

C# WinForm进

2009-08-27 17:40:21

C#接口的作用

2009-08-28 12:31:06

C#静态方法

2009-09-04 13:37:44

C#货币格式
点赞
收藏

51CTO技术栈公众号