gpt4 book ai didi

c# - Winforms 用户控件自定义事件

转载 作者:IT王子 更新时间:2023-10-29 04:32:25 24 4
gpt4 key购买 nike

有没有办法给用户控件自定义事件,并在用户控件内的事件上调用该事件。 (我不确定调用是否正确)

public partial class Sample: UserControl
{
public Sample()
{
InitializeComponent();
}


private void TextBox_Validated(object sender, EventArgs e)
{
// invoke UserControl event here
}
}

和 MainForm:

public partial class MainForm : Form
{
private Sample sampleUserControl = new Sample();

public MainForm()
{
this.InitializeComponent();
sampleUserControl.Click += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
// do stuff
}
}

最佳答案

除了 Steve 发布的示例之外,还有可用的语法可以简单地传递事件。它类似于创建属性:

class MyUserControl : UserControl
{
public event EventHandler TextBoxValidated
{
add { textBox1.Validated += value; }
remove { textBox1.Validated -= value; }
}
}

关于c# - Winforms 用户控件自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2188059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com