C#跨窗体调用控件(委托回调函数使用例子)
问题: 有两个窗体,FORM1(含一个label控件,一个名为显示form2的button控件)和FORM2(含一个button控件)。启动时,FORM1中点击button控件显示form2使FORM2显示出来。点击FORM2中的button1后,Form1中的label1的text改变。 解决:用事件回调 一、在Form2
解决:用事件回调
一、在Form2里面:
首先声明一个委托和委托实例
public delegate void SetMainFormTopMostHandle(bool topmost);
public event SetMainFormTopMostHandle SetMainFormTopMost;
二、在Form1里面:
button控件“显示form2“的click事件中有下面的代码:
{
Form2 f2 = new Form2 ();
f2.Show()
}
然后在f2.Show()之前,加一句:
f2.SetMainFormTopMost += new SetMainFormTopMostHandl(f2_SetMainFormTopMost); 用来给form1中的SetMainFormTopMost注册一个修改label控件内容的方法f2_SetMainFormTopMost()
提示:
f2.SetMainFormTopMost += new SetMainFormTopMostHandle(f2_SetMainFormTopMost);
这句最关键,你输入到+=之后,按两下Tab,他会自动给你生成回调函数
如下:
void f2_SetMainFormTopMost(bool topmost)
{
label1.Text = "调用成功";//改变label内容的代码
}
这里面f2_SetMainFormTopMost就是你用来修改label的方法(回调函数)。 SetMainFormTopMostHandle、SetMainFormTopMost分别是定义的委托和委托实例名称,自己可以随便定义。
大致就是为f2创建一个用来修改label的方法,然后把该方法绑定到这个委托实例上。
三、启动f2,在f2中执行这个委托实例,就是SetMainFormTopMost(true)这样写 (参数要和委托的那个声明一 致)
那么就会调用F1中绑定的那个方法。
源代码如下:
Form1中的代码:
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 跨窗体调用控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void f2_SetMainFormTopMost(bool topmost) //回调函数
{
label1.Text = "调用成功";
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.SetMainFormTopMost += new SetMainFormTopMostHandle(f2_SetMainFormTopMost);//给form2中的委托实例SetMainFormTopMost 注册方法
f2.Show();
}
}
}
Form2中的代码:
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 跨窗体调用控件
{
public delegate void SetMainFormTopMostHandle(bool topmost); //定义委托
public partial class Form2 : Form
{
public SetMainFormTopMostHandle SetMainFormTopMost;//定义委托实例
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SetMainFormTopMost(true);//执行委托实例
}
}
}
C#跨窗体调用控件(委托回调函数使用例子)的更多相关文章
- C# 委托实例(跨窗体操作控件)
在C#里面却是可以不用自定义消息这么复杂的方法来实现跨窗体调用控件,C#有更好的办法就是委托. 效果描述:有两个窗体,FORM1(一个名为“打开form2”的button控件)和FORM2(一个名为“ ...
- Winform跨窗体操作控件(使用委托)
Winform跨窗体操作控件是winform开发中很常见的形式,最常见且简单有效的方式便是使用委托的方式来进行操作,下面我将通过一个小实例来说明如何使用委托跨窗体实现控件操作. 实例介绍:两个窗体,F ...
- C# 跨线程调用控件
在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应. 同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线程间操作无效 第一种办法:禁 ...
- 【转载】C# 跨线程调用控件
转自:http://www.cnblogs.com/TankXiao/p/3348292.html 感谢原作者,转载以备后用 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停 ...
- C# 跨线程调用控件的4中方法
原文:C# 跨线程调用控件 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应. 同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线 ...
- 跨线程调用控件之MethodInvoker
原文:http://www.cnblogs.com/cm8448940/archive/2008/07/10/1240045.html 使用到两个控件,一个按钮button1,一个标签label1. ...
- winform 跨窗体给控件传值 分类: WinForm 2014-08-02 16:33 195人阅读 评论(0) 收藏
两个窗体 FormA,FormB; FormA窗体中有一文本框控件:TextBox; FormB窗体中有一变量:txtJSJ 目的:把变量赋值给文本框 实现: 设置TextBox属性: Modifie ...
- swiper控件(回调函数)
来源 属性: swiper.slides.length 1.onInit(swiper): function(){...} swiper初始化完成,会调回调 onInit 方法 获取当前swiper ...
- WinForm与WPF下跨线程调用控件
Winform下: public delegate void UpadataTextCallBack(string str,TextBox text); public void UpadtaText( ...
随机推荐
- ASP.NET MVC性能优化工具 MiniProfiler
ASP.NET MVC性能优化工具 MiniProfiler 2014年04月19日 ⁄ ASP.NET ⁄ 共 1159字 ⁄ 字号 小 中 大 ⁄ 暂无评论 ⁄ 阅读 325 views 次 MV ...
- poj3070--Fibonacci(矩阵的高速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9650 Accepted: 6856 Descrip ...
- Android Notification通知详细解释
Android Notification通知具体解释 Notification: (一).简单介绍: 显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...
- centos安装zabbix集群监控(亲测无坑版)
一. 安装lemp环境 下载安装包:wget bbs.linuxtone.org/docs/autoinstall/lemp_auto_v1.0.6.tar.gz 包解压:tar zxvf lemp_ ...
- Oracle SqlPlus 方向键的方法和解决的退格键失效
SqlPlus中退格键和方向键的设置 在刚装好的Oracle中,我们使用SqlPlus会发现很的蹩脚,不仅退格键不好用,方向键也不行调出history.以下有几种解决方法. 1.能够使用ctrl+Ba ...
- CentOS上部署Apache、MySQL和PHP
centos上yum安装很方便,下面介绍编译安装的方式. 第一步要在CentOS上安装gcc.g++等开发工具 可以从系统光盘上安装,或者 #yum groupinstall "Develo ...
- Java集合之ArrayList源码分析
1.简介 List在数据结构中表现为是线性表的方式,其元素以线性方式存储,集合中允许存放重复的对象,List接口主要的实现类有ArrayList和LinkedList.Java中分别提供了这两种结构的 ...
- JQuery Smart UI
JQuery Smart UI 个人开发的一套使用htm+js的开发框架 SmartUI2.0后续声明 摘要: 感谢很多朋友关注,因为今年一直在另外一个公司做顾问,网络环境管制相当严格,所以一直没有更 ...
- mysql查询字段值为数字
原文:mysql查询字段值为数字 我想查询字段值为数字的sql如下:select * from tj_item_result where tj_value REGEXP '^[0-9]'
- CLR Profiler 性能分析工具
CLR Profiler 性能分析工具 CLR Profiler 性能分析工具 CLR Profiler 有两个版本,分别用于CLR1.1 和 CLR2.0,至于CLR4试了一些也可以,但不知道是否完 ...