• c#中在一个窗体中触发另一个窗体的事件


    c#中在一个窗体中触发另一个窗体的事件
    创建两个窗体,分别为form1,form2,在form1中添加控件textBox1和button1,创建一个form2的对象Form2 b = null;
    在form2中添加button1,定义委托和事件
     //定义委托
            public delegate void MyDelegate();
            //定义事件
            public event MyDelegate MyEvent;
    给form2中的button1添加消息相应函数并做修改 
         private void button1_Click(object sender, EventArgs e)
            {
                if (MyEvent != null)
                    MyEvent();//引发事件
                this.Close();
            }
    在form1的代码中添加函数
         void b_MyEvent()
                {
                    this.textBox1.Text += "已单击b窗体按钮
    ";
                }
    修改form1的构造函数
         public Form1()
                {
                    InitializeComponent();
                    b = new Form2();//实例化b窗体
                    b.MyEvent += new Form2.MyDelegate(b_MyEvent);//监听b窗体事件
                }
    为form1中的button1添加消息响应函数
        private void button1_Click(object sender, EventArgs e)
                {
                    b.ShowDialog();
    
                }
    这样当单击form1中的按钮时会弹出form2,当单击form2中的按钮时,form1中的textbox1会显示“已单击b窗体按钮”。
    具体代码如下(vs 2005实现):
    
    form1代码:
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace form1
    {
    
            public partial class Form1 : Form
            {
                Form2 b = null;
    
                public Form1()
                {
                    InitializeComponent();
    
                    b = new Form2();//实例化b窗体
                    b.MyEvent += new Form2.MyDelegate(b_MyEvent);//监听b窗体事件
                }
    
    
                void b_MyEvent()
                {
                    this.textBox1.Text += "已单击b窗体按钮
    ";
                }
    
                private void button1_Click(object sender, EventArgs e)
                {
                    b.ShowDialog();
    
                }
    }
    }
    
    form2代码:
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace form1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            //定义委托
            public delegate void MyDelegate();
            //定义事件
            public event MyDelegate MyEvent;
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (MyEvent != null)
                    MyEvent();//引发事件
                this.Close();
            }
    
        }
    }
  • 相关阅读:
    程序员 你中毒了吗?
    Win8 下安装 Live Writer 发布博客
    Rational Rose 2003 下载及破解方法(转载)
    如何在dos 下使用csc.exe命令?
    as 与 is
    【转载】关于工资的三个秘密
    C#反射(1)<转>
    C#常用字符串格式
    微软企业库EntLib5.0使用过程中常见的异常
    关于window7 AERO 声音 IIS 无线网络失效的解决办法
  • 原文地址:https://www.cnblogs.com/ck235/p/4939140.html
Copyright © 2020-2023  润新知