C#利用委托实现窗体间的值传递
C#利用委托实现窗体间的值传递
A、网上有很多方法,大家可搜一下,都可用。
B、这里只是说明下是只利用委托,学习基本的委托运用。
方法如下:
1、C#建立一个默认工程,默认窗体Form1
2、加入一个新窗体,默认Form2
3、Form1窗体上放一个Label,一个Button,属性分别为:
this.label1.Text = ""; // 清空label的值
this.button1.Text="otherForm"; //设置button显示为otherForm
view plaincopy to clipboardprint?
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.label1.Text = "";
this.button1.Text = "otherForm";
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.label1.Text = "";
this.button1.Text = "otherForm";
}
4、Form2窗体上放一个textbox1,一个Button,属性分别为:
this.textbox1.Text = ""; //清空textbox的值
this.button1.Text = "input"; //设置button显示为input(输入)
view plaincopy to clipboardprint?
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox1.Text = "";
this.button1.Text = "input";
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox1.Text = "";
this.button1.Text = "input";
}
5、上面的步骤就建立了两个基本的窗体,接下来回到Form1的代码设计视图
拷贝下面代码
view plaincopy to clipboardprint?
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.label1.Text = "";
this.button1.Text = "otherForm";
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
returnValueEvent += new returnValue(Form1_returnValueEvent);
frm2.Show();
}
void Form1_returnValueEvent(string str)
{
// throw new NotImplementedException();
this.label1.Text = str;
}
// 定义委托
public delegate void returnValue(string str);
// 委托的事件
public static event returnValue returnValueEvent;
// 委托的事件的函数
public void doReturnValue(string str)
{
returnValueEvent(str);
}
}
}
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.label1.Text = "";
this.button1.Text = "otherForm";
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
returnValueEvent += new returnValue(Form1_returnValueEvent);
frm2.Show();
}
void Form1_returnValueEvent(string str)
{
// throw new NotImplementedException();
this.label1.Text = str;
}
// 定义委托
public delegate void returnValue(string str);
// 委托的事件
public static event returnValue returnValueEvent;
// 委托的事件的函数
public void doReturnValue(string str)
{
returnValueEvent(str);
}
}
}
6、在Form2的代码设计视图中,拷贝下面代码
view plaincopy to clipboardprint?
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox1.Text = "";
this.button1.Text = "input";
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.doReturnValue(this.textBox1.Text);
}
}
}
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox1.Text = "";
this.button1.Text = "input";
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.doReturnValue(this.textBox1.Text);
}
}
}
7、运行后,就可以在两个窗体间传递参数了
注:重点是第六步中的,对委托的定义和调用
returnValueEvent += new returnValue(Form1_returnValueEvent);
void Form1_returnValueEvent(string str)
{
// throw new NotImplementedException();
this.label1.Text = str;
}
// 定义委托
public delegate void returnValue(string str);
// 委托的事件
public static event returnValue returnValueEvent;
// 委托的事件的函数
public void doReturnValue(string str)
{
returnValueEvent(str);
}
在Form2中的调用,在按钮的click事件中
Form1 frm1 = new Form1();
frm1.doReturnValue(this.textBox1.Text);
C#利用委托实现窗体间的值传递相关推荐
- 利用委托 实现窗体间通信,非原创
copy自:https://www.cnblogs.com/hugoNB/p/7130562.html,这个作者写的浅显易懂,就复制下来自己看 实现过程: 这里主要是用到委托实现,所以主要描述一下委托 ...
- C#不同窗体间通信,数据传递
在一个项目中,很多时候都需要在窗体间进行数据传递和通信,最觉见的是父子窗体之间的数据传递,比如登录ID,各个窗体都需要知道.有很多文章都写了这方面的问题,提出很多优秀的方法,鄙人不才,搜了一些资料之后 ...
- php函数间参数传递(值传递/引用传递)
为什么80%的码农都做不了架构师?>>> 函数间的参数传递包括值传递和引用传递. php:函数间的参数传递 1.值传递 复制代码代码如下: <?php function ...
- C#中两个窗体间的数据传递
winform 窗体编程 在两个窗体间传递数据可分为两种: 一:父窗体向子窗体传递. 二:子窗体向父窗体传递. 传递实现的机制: 分别在两个窗体中创建公有属性的设置字段,分别供两个窗口调用. 两种情况 ...
- jsp与jsp页面间的值传递与接收
1.使用<a>标签 传递值 <a href="index.jsp?name=增加数据">增加数据</a> ///目标页面/值/// 接收值 &l ...
- 利用事件进行窗体间传值
为什么80%的码农都做不了架构师?>>> 举例,现在有两个窗体,SelectForm窗体查询信息集合,使用DataGridview显示信息集合,EditForm窗体添加和修改信 ...
- 用委托实现窗体间传值
1.新建一个工程.在Form1中添加一个Label和一个Button.新建一个事件类,让它有一个string 类型的属性,用于传值. 1 ///ReturnValueEventArgs.cs 2 u ...
- C# 利用委托事件进行窗体间的传值
引言: 窗体间传值是每个学习WinForm新手的常见问题,最初级的方法就是 在窗体中先获取到要接受值窗体.然后通过.得到某个空间或者属性,直接赋值,这个需要接收放的窗体属性或者空间必须是public ...
- WinForm窗体间如何传值
窗体间传递数据,无论是父窗体操作子窗体,还是子窗体操作符窗体,有以下几种方式: 公共静态变量: 使用共有属性: 使用委托与事件: 通过构造函数把主窗体传递到从窗体中: 一.通过静态变量 特点:传值是双 ...
最新文章
- left join 一对多_MYSQL 连接查询算法:JOIN语句在 MYSQL 内部到底是怎么执行的
- Ms SQL Server 约束和规则
- 捕获异常VS抛出异常
- sql server 自定义函数的使用
- python package_python之package定义
- 有什么办法可以判断页面是静态还是动态?_你知道seo到底是什么吗?该怎么优化?...
- Activiti的流程定义文件解析
- CentOS 缺少mysqli扩展 请检查PHP配置
- linux网络编程--服务器模型(epoll/select/poll)
- java socket 出现丢包_Java知识——网络编程、三次握手四次挥手
- 前端学习(一):HTML基本语法
- XSS跨站脚本攻击详解(包括攻击方式和防御方式)
- 荣耀笔记本linux版拆机,荣耀MagicBook Pro 16.1拆机有惊喜,官方都没提,居然能拓展...
- (什么是 RPC?) AND(什么是域名?)AND (DMA是什么?)AND(IRQ是什么?) 选择自 ycool1984 的 Blog
- Arcgis小技巧【3】——计算图斑的投影面积和椭球面积
- 安装图形化界面consol/centos-xfce-vnc
- 黑客历程碑事件编年史
- 腾讯云TVP大佬十年心血MySQL工作笔记,看完还不懂MySQL来打我!
- 刷爆力扣之字符串中的单词数
- 微服务 中台架构演进