C#程序窗体间使用回调事件方式通讯示例
更新时间:2020年6月25日 11:39 点击:1975
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);
}
}
上一篇: OpenXml读写Excel实例代码
下一篇: C#传值方式实现不同程序窗体间通信实例
相关文章
- C#有事件这个东西,为啥不用呢,而且事件在窗体通信方面,有着更为方便的作用,我们知道事件实际上就是状态的捕获,数据互相操作的例子...2020-06-25
- Form2构造函数中接收一个string类型参数,即Form1中选中行的文本,将Form2的TextBox控件的Text设置为该string,即完成了Form1向Form2的传值...2020-06-25
- 钉钉有回调事件流程,本文主要介绍了python注册钉钉回调事件的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09