一、方法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# 学习]窗体间调用控件的更多相关文章

  1. C#跨窗体调用控件(委托回调函数使用例子)

    问题: 有两个窗体,FORM1(含一个label控件,一个名为显示form2的button控件)和FORM2(含一个button控件).启动时,FORM1中点击button控件显示form2使FORM ...

  2. 【机房系统知识小结点系列】之遍历窗体中的控件,判断Text是否为空?

    做机房系统时,几乎每个窗体中都会用到判断界面中的控件是否为空的情景.我们曾经是这样走来的: 第一版: 好处:对窗体界面中的Text等控件,逐一做判断,当用户输入某一项为空的时候,会议弹出框的形式,告诉 ...

  3. C# 跨线程调用控件

    在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应.  同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线程间操作无效 第一种办法:禁 ...

  4. [译]- 6-1 排列窗体上的控件(Laying Out Widgets on a Form)

     排列窗体上的控件(Laying Out Widgets on a Form) 中英文对照:form(窗体),layout(布局或者排列,意思是进行窗体上控件的排列的过程,如大小位置等) absolu ...

  5. WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案

    首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如  WPF中不规则窗体与WebBrowser控件的兼 ...

  6. 【转载】C# 跨线程调用控件

    转自:http://www.cnblogs.com/TankXiao/p/3348292.html 感谢原作者,转载以备后用 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停 ...

  7. 使用Invoke解决多线程间的控件访问出错

    // 按钮点击事件处理程序private void button1_Click(object sender, EventArgs e){    //创建新线程    Thread processorT ...

  8. C# 跨线程调用控件的4中方法

    原文:C# 跨线程调用控件 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应.  同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线 ...

  9. Android 使用代码主动去调用控件的点击事件(模拟人手去触摸控件)

    使用代码主动去调用控件的点击事件(模拟人手去触摸控件) //View 可以是LinearLayout,Button,TextView View.performClick();

随机推荐

  1. CF1093B Letters Rearranging 题解

    Content 有 \(t\) 次询问,每次询问给定一个字符串 \(s\).定义一个"好的字符串"为不是回文串的字符串.对于每一次询问,求出任意一个重新排列能够得到的"好 ...

  2. C++实现反射---RTTR库的使用

    使用过C#或者Java 的童鞋,应该对这些语言提供的反射机制有所了解.所谓反射,在我看来就是在只知道一个类的名字(字符串形式)的情况下,自动创建出具体的类实例,并且能够枚举该类型拥有的属性.方法等信息 ...

  3. jQuery Validate表单验证判断是否验证通过

    只判断某个字段是否验证通过,可以参考:https://www.cnblogs.com/pxblog/p/13801171.html <form action="" metho ...

  4. IDEA 修改之前保存的git地址的账号和密码

    1.打开控制面板 快捷键 win+R,然后输入control,打开控制面板 2.用户账户 3.管理windows凭据 4.点击里面的git就可以修改了

  5. SpringBoot整合Apache Shiro

    Subject 用户主体 (把操作交给SecurityManager)SecurityManager 安全管理器 (关联Realm)Realm   Shiro连接数据的桥梁 引入maven依赖 < ...

  6. 【九度OJ】题目1175:打牌 解题报告

    [九度OJ]题目1175:打牌 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1175 题目描述: 牌只有1到9,手里拿着已经排好序的 ...

  7. 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  8. 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...

  9. 15 - Vue3 UI Framework - 完工部署

    项目官网也基本完成了,接下来我们再讲一下如何将项目官网部署到 GitHub Pages 上 返回阅读列表点击 这里 项目配置 常见的模式有三种,即 History 模式 Hash 模式 Memory ...

  10. python xlwt写Excel表

    1 xlwt第三方库 说明:xlwt是一个用于将数据和格式化信息写入并生成Excel文件的库. 注意:xlwt不支持写xlsx表,打开表文件报错. 官方文档:https://xlwt.readthed ...