[C# 学习]窗体间调用控件
一、方法1:
假如有两个窗体,Form_A和Form_B,每个窗体里都有一个按键,Button_A和Button_B,要实现单击Button_A显示窗体B,那么窗体A中Buttom_A的单击事件的程序应该是:
private void button_A_Click(object sender, EventArgs e)
{
Form_B f = new Form_B();
f.Show();
}
如果希望单击窗体B中的按键Button_B,实现改变窗体A的背景色,那么你需要做:
1. Form_B 窗体的Class里添加如下代码:
public Form_A fb;
public Form_B(Form_A f)
{
InitializeComponent();
fb = f;
} private void button_B_Click(object sender, EventArgs e)
{
fb.BackColor = Color.Brown;
}
2. Form_A窗体中的Button_A单击事件变成:
private void button_A_Click(object sender, EventArgs e)
{
Form_B f = new Form_B(this);
f.Show();
}
完整程序如下
Form_A:
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 formExam
{
public partial class Form_A : Form
{
public Form_A()
{
InitializeComponent();
} private void Button_A_Click(object sender, EventArgs e)
{
Form_B f = new Form_B(this);
f.Show();
} private void Form_A_Load(object sender, EventArgs e)
{ }
}
}
Form_B:
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 formExam
{
public partial class Form_B : Form
{
public Form_A fb;
public Form_B(Form_A f)
{
InitializeComponent();
fb = f;
} private void Button_B_Click(object sender, EventArgs e)
{
fb.BackColor = Color.Brown;
}
}
}
二、方法2:通过委托实现
1. 在Form_B的Class外定义一个委托类型
public delegate void ChangeFormColor();
2. 在Form_B的Class内定义委托的方法
public event ChangeFormColor ChangeColor;
3. Button_B单击事件为
private void Button_B_Click(object sender, EventArgs e)
{
ChangeColor();
}
4. Form_A中的单击事件为
private void Button_A_Click(object sender, EventArgs e)
{
Form_B f = new Form_B();
f.ChangeColor += new ChangeFormColor(f_ChangeColor);
f.Show();
}
5. 编写改变Form_A 背景色的方法
void f_ChangeColor()
{
this.BackColor = Color.Brown;
}
完整程序如下:
Form_A:
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 formExam
{
public partial class Form_A : Form
{
public Form_A()
{
InitializeComponent();
} private void Button_A_Click(object sender, EventArgs e)
{
Form_B f = new Form_B();
f.ChangeColor += new ChangeFormColor(f_ChangeColor);
f.Show();
} void f_ChangeColor()
{
this.BackColor = Color.Brown;
}
}
}
Form_B:
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 formExam
{
public delegate void ChangeFormColor(); public partial class Form_B : Form
{
public event ChangeFormColor ChangeColor;
public Form_B()
{
InitializeComponent();
} private void Button_B_Click(object sender, EventArgs e)
{
ChangeColor();
}
}
}
[C# 学习]窗体间调用控件的更多相关文章
- C#跨窗体调用控件(委托回调函数使用例子)
问题: 有两个窗体,FORM1(含一个label控件,一个名为显示form2的button控件)和FORM2(含一个button控件).启动时,FORM1中点击button控件显示form2使FORM ...
- 【机房系统知识小结点系列】之遍历窗体中的控件,判断Text是否为空?
做机房系统时,几乎每个窗体中都会用到判断界面中的控件是否为空的情景.我们曾经是这样走来的: 第一版: 好处:对窗体界面中的Text等控件,逐一做判断,当用户输入某一项为空的时候,会议弹出框的形式,告诉 ...
- C# 跨线程调用控件
在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应. 同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线程间操作无效 第一种办法:禁 ...
- [译]- 6-1 排列窗体上的控件(Laying Out Widgets on a Form)
排列窗体上的控件(Laying Out Widgets on a Form) 中英文对照:form(窗体),layout(布局或者排列,意思是进行窗体上控件的排列的过程,如大小位置等) absolu ...
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如 WPF中不规则窗体与WebBrowser控件的兼 ...
- 【转载】C# 跨线程调用控件
转自:http://www.cnblogs.com/TankXiao/p/3348292.html 感谢原作者,转载以备后用 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停 ...
- 使用Invoke解决多线程间的控件访问出错
// 按钮点击事件处理程序private void button1_Click(object sender, EventArgs e){ //创建新线程 Thread processorT ...
- C# 跨线程调用控件的4中方法
原文:C# 跨线程调用控件 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应. 同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线 ...
- Android 使用代码主动去调用控件的点击事件(模拟人手去触摸控件)
使用代码主动去调用控件的点击事件(模拟人手去触摸控件) //View 可以是LinearLayout,Button,TextView View.performClick();
随机推荐
- LuoguP7222 [RC-04] 信息学竞赛 题解
Content 给定一个角 \(\alpha\),求 \(\beta=90^\circ-\alpha\). 数据范围:\(\alpha\in[-2^{31},2^{31}-1]\). Solution ...
- mysql联合查询更新数据库例子
mysql联合查询更新数据库例子,用户表,部门表,把用户表中的部门属性更新为部门表的主键UPDATE user_table AS utINNER JOIN belongdept AS bd ON bd ...
- vue中的数据代理原理
const vm = new Vue({ data:{ name:'boos' } }) // 注意 :使用构造函数构建vue实例时,传入的是一个option对象,它包含了data,computed等 ...
- 【LeetCode】536. Construct Binary Tree from String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计字符串出现的次数 日期 题目地址:https:// ...
- 【九度OJ】题目1018:统计同成绩学生人数 解题报告
[九度OJ]题目1018:统计同成绩学生人数 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1018 题目描述: ...
- 【LeetCode】290. Word Pattern 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 为什么我的 WordPress 网站被封了?
今年以来,一系列 "清朗" "护苗" "净网" 专项整治行动重拳出击,"清朗·春节网络环境"取消备案网站平台2300余家 ...
- MongoDB笔记:windows环境安装及连接本地数据库
下载MongoDB 2.4.9版 mongodb官网下载:http://www.mongodb.org/downloads 直接下载地址:http://fastdl.mongodb.org/win32 ...