[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();
随机推荐
- LuoguB2106 矩阵转置 题解
Content 给定一个 \(n\times m\) 的矩阵 \(A\),求其转置 \(A^\text T\). 数据范围:\(1\leqslant n,m\leqslant 100\). Solut ...
- linux test使用
文件 文件是否存在 test -f 判断文件是否存在 test -d 目录是否存在 test -e 文件名是否存在 通过echo $? 来得知test后的结果 test -f sh && ...
- Linux的课堂便利脚本
上课的时,因为教室机总会重新重启,有时候就要重配网卡yum源和下载一些辅助工具,这里写一个脚本省去冗杂的过程 if [[]]可以防止unary operator expected的报错 nmcli d ...
- React使用css module和className多类名设置
引入样式文件 import styles from './footer.module.css'; 注意: 样式文件名必须要以.module.css结尾 单类名设置 <div className= ...
- Linux执行脚本报错:-bash: ./xx.sh: /bin/bash^M: bad interpreter: No such file or directory
1.用vim打开文本 输入 : set ff 这里要先按":"号 显示文件为dos格式 2.强制装换格式为unix 先按冒号":" set ff=unix 然后 ...
- Linux(Centos)配置vsftp使用账号密码(虚拟用户)登录ftp进行文件上传和修改
安装vsftp yum install vsftpd -y 安装完成之后进入vsftp的配置文件夹 cd /etc/vsftpd/ 文件夹内容如下 [root@VM-0-12-centos vsftp ...
- 优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download
优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download TTFB全称Time To First Byte,是指网络请求被发起到从服务器接收到地 ...
- ubuntu查系统信息及系统服务
系统信息 uname -a # 查看内核/操作系统/CPU信息 cat /etc/issue # 查看操作系统版本 cat /proc/version ...
- num_duilib之TabBox用法(21)
介绍 本文将介绍 使用 TabBox的用法 更多用法,请参考源码 TabBox.h 文件 文件中定义了公有接口,其中包括,添加tab页下的控件,删除.设置TabBox的属性 其中,我常用的有:GetC ...
- C. Tourist's Notes
C. Tourist's Notes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...