加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#程序窗体间使用回调事件方式通讯示例

发布时间:2020-12-15 05:59:38 所属栏目:百科 来源:网络整理
导读:Form2: 复制代码 代码如下: //定义一个需要string类型参数的委托 publicdelegate void MyDelegate(string text); public partial class Form2 :Form1 { //定义该委托的事件 public event MyDelegate MyEvent; public Form2(string text) { InitializeCompon

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.Items.RemoveAt(index);    
            this.listBox1.Items.Insert(index,text);    
       }    
   }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读