zoukankan      html  css  js  c++  java
  • C#事件传参数[delegate]

    事件回调 既然C#有事件这个东西,为啥不用呢,而且事件在窗体通信方面,有着更为方便的作用,我们知道事件实际上就是状态的捕获,在最后我会举一个捕获状态的例子,先看数据互相操作的例子。 Form2:
    //定义一个需要string类型参数的委托
    publicdelegate void MyDelegate(string text);
    public partial class Form2 :Form1
    {
    //定义该委托的事件
    public event MyDelegate MyEvent;
    public Form2(string text)
    {
    InitializeComponent();
    this.textBox1.Text = text;
    }
    private void btnChange_Click(object sender, EventArgs e)
    {
    //触发事件,并将修改后的文本回传
    MyEvent(this.textBox1.Text);
    this.Close();
    }
    }

    Form1:public partial class Form1 :Form
    {
    public int index = 0;
    public string text = null;
    public Form1()
    {
    InitializeComponent();
    }
    private void listBox1_SelectedIndexChanged(object sender, EventArgse)
    {
    if (this.listBox1.SelectedItem != null)
    {
    text = this.listBox1.SelectedItem.ToString();
    index = this.listBox1.SelectedIndex;
    Form2 form2 = new Form2(text);
    //注册form2_MyEvent方法的MyEvent事件
    form2.MyEvent += new MyDelegate(form2_MyEvent);
    form2.Show();
    }
    }
    //处理
    void form2_MyEvent(string text)
    {
    this.listBox1.
  • 相关阅读:
    第一章 数据集散地:数据库
    第六章 使用ADO.NET查询和操作数据
    第五章 使用ADO.NET访问数据库
    第四章 深入C#的String类
    IOS框架和服务
    一步步调试解决iOS内存泄漏
    app跳转
    iOS 视频直播
    学习心得
    iOS中FMDB的使用
  • 原文地址:https://www.cnblogs.com/itelite/p/2348954.html
Copyright © 2011-2022 走看看