[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();
随机推荐
- java 图形化小工具Abstract Window Toolit :画笔Graphics,画布Canvas(),弹球小游戏
画笔Graphics Java中提供了Graphics类,他是一个抽象的画笔,可以在Canvas组件(画布)上绘制丰富多彩的几何图和位图. Graphics常用的画图方法如下: drawLine(): ...
- Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】
Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...
- 在myeclipse里加大tomcat内存,jdk内存方法
这是在myeclipse里加大的方法: -Xms4096m -Xmx4096m -XX:MaxNewSize=4096m -XX:MaxPermSize=4096m 如图所示: -XX:PermSiz ...
- Windows库函数获取 可执行程序所在路径
头文件 #include <Windows.h> 函数 函数已经写好了 std::string get_executable_dir_() { char path[255] = { 0 } ...
- qt5之使用QtXlsxWriter库
note Qt version: 5.12 platform: os x 10.15 本文将介绍直接使用QtXlsxWriter源码 准备 下载QtXlsxWriter 使用Qt Creator 创建 ...
- c++之可变参数格式化字符串(c++11可变模板参数)
本文将使用 泛型 实现可变参数. 涉及到的关见函数: std::snprintf 1.一个例子 函数声明及定义 1 // 泛型 2 template <typename... Args> ...
- 【LeetCode】513. Find Bottom Left Tree Value 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS Date 题目地址:https:// ...
- 【剑指Offer】二叉搜索树与双向链表 解题报告(Python)
[剑指Offer]二叉搜索树与双向链表 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interview ...
- 从零搭建react hooks项目(github有源代码)
前言 首先这是一个react17的项目,包含项目中常用的路由.状态管理.less及全局变量配置.UI等等一系列的功能,开箱即用,是为了以后启动项目方便,特地做的基础框架,在这里分享出来. 这里写一下背 ...
- [opencv]利用minAreaRect计算平面矩形的旋转角度
#include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include ...