在另一个事件处理程序中调用一个事件处理程序?

发布于 2024-12-01 16:50:07 字数 312 浏览 1 评论 0原文

这是简短的示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜泪 2024-12-08 16:50:07

您可以这样做 - 尽管您提供的代码无法编译。它应该看起来像这样:

private void txtbox1_DoubleClick(object sender, EventArgs e)
{
    button1_Click(sender, e);
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(txtbox1.Text);
}

但是为了最佳实践和代码可读性,您最好这样做,特别是当您不使用 sendere 时:

private void txtbox1_DoubleClick(object sender, EventArgs e)
{
    ShowMessageBox();
}

private void button1_Click(object sender, EventArgs e)
{
    ShowMessageBox();
}

private void ShowMessageBox()
{
    MessageBox.Show(txtbox1.Text);
}

You can do that - although the code you provide can't be compiled. It should look like this:

private void txtbox1_DoubleClick(object sender, EventArgs e)
{
    button1_Click(sender, e);
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(txtbox1.Text);
}

But for best practice and code readability, you're probably better off doing this, especially as you are not making use of sender and e:

private void txtbox1_DoubleClick(object sender, EventArgs e)
{
    ShowMessageBox();
}

private void button1_Click(object sender, EventArgs e)
{
    ShowMessageBox();
}

private void ShowMessageBox()
{
    MessageBox.Show(txtbox1.Text);
}
只涨不跌 2024-12-08 16:50:07

是的,你可以这样做;事件处理程序只是另一种方法。

但是,可能值得创建一个显示消息框的新方法,并让两个 Click 事件处理程序都调用该方法:

private void txtbox1_DoubleClick(object sender, EventArgs e)
{
    ShowTextboxMessage();
}

private void button1_Click(object sender, EventArgs e)
{
    ShowTextboxMessage();
}

private void ShowTextboxMessage()
{
    MessageBox.Show(txtbox1.Text);
}

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:

private void txtbox1_DoubleClick(object sender, EventArgs e)
{
    ShowTextboxMessage();
}

private void button1_Click(object sender, EventArgs e)
{
    ShowTextboxMessage();
}

private void ShowTextboxMessage()
{
    MessageBox.Show(txtbox1.Text);
}
趁年轻赶紧闹 2024-12-08 16:50:07

事件处理程序只不过是一个方法,因此您可以像调用其他方法一样调用它。

An event handler is nothing more than a method, so you can call it like any other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文