在另一个事件处理程序中调用一个事件处理程序?
这是简短的示例代码:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(object sender, EventArgs e); //can I call button1 event handler?
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
我想知道用上面的方式编码是否可以?
Here is the short sample code:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(object sender, EventArgs e); //can I call button1 event handler?
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
I wonder if it would be okay to code in the above way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以这样做 - 尽管您提供的代码无法编译。它应该看起来像这样:
但是为了最佳实践和代码可读性,您最好这样做,特别是当您不使用
sender
和e
时:You can do that - although the code you provide can't be compiled. It should look like this:
But for best practice and code readability, you're probably better off doing this, especially as you are not making use of
sender
ande
:是的,你可以这样做;事件处理程序只是另一种方法。
但是,可能值得创建一个显示消息框的新方法,并让两个 Click 事件处理程序都调用该方法:
Yes you can do that; an event handler is just another method.
However it might be worth creating a new method that shows the message box, and having both Click event handlers call that:
事件处理程序只不过是一个方法,因此您可以像调用其他方法一样调用它。
An event handler is nothing more than a method, so you can call it like any other.