C#程序窗体间使用回调事件方式通讯示例
2023-06-13 09:15:13 时间
Form2:
//定义一个需要string类型参数的委托
publicdelegatevoidMyDelegate(stringtext);
publicpartialclassForm2:Form1
{
//定义该委托的事件
publiceventMyDelegateMyEvent;
publicForm2(stringtext)
{
InitializeComponent();
this.textBox1.Text=text;
}
privatevoidbtnChange_Click(objectsender,EventArgse)
{
//触发事件,并将修改后的文本回传
MyEvent(this.textBox1.Text);
this.Close();
}
}
Form1:
publicpartialclassForm1:Form
{
publicintindex=0;
publicstringtext=null;
publicForm1()
{
InitializeComponent();
}
privatevoidlistBox1_SelectedIndexChanged(objectsender,EventArgse)
{
if(this.listBox1.SelectedItem!=null)
{
text=this.listBox1.SelectedItem.ToString();
index=this.listBox1.SelectedIndex;
Form2form2=newForm2(text);
//注册form2_MyEvent方法的MyEvent事件
form2.MyEvent+=newMyDelegate(form2_MyEvent);
form2.Show();
}
}
//处理
voidform2_MyEvent(stringtext)
{
this.listBox1.Items.RemoveAt(index);
this.listBox1.Items.Insert(index,text);
}
}
相关文章
- c#启动外部程序_winform程序自动更新
- C#多线程同步事件及等待句柄
- C# WPF DataGrid下面 使用CheckBox 选中事件
- asp.net(C#)生成随机验证码的代码
- 用C#写的ADSL拨号程序的代码示例
- c#委托与事件(详解)
- C#委托(delegate)的小例子
- 解决C#中WebBrowser的DocumentCompleted事件不执行的实现方法
- 解析c#操作excel后关闭excel.exe的方法
- C#中怎么将一个List转换为只读的
- C#的锯齿数组以及C++实现代码
- c#二进制逆序方法详解
- c#远程html数据抓取实例分享
- c#使用process.start启动程序报错解决方法
- c#注册客户端事件示例
- c#事件使用示例详解
- C#实现发送邮件的三种方法
- C#中线程同步对象的方法分析
- C#中自定义事件和委托实例
- C#中的事件介绍
- C#启动外部程序的几种常用方法汇总
- C#通过反射创建自定义泛型
- C#Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法
- C#实现自定义双击事件