gpt4 book ai didi

c# - 我想在另一个事件中调用一个 visual c# 事件。我怎么做?

转载 作者:太空宇宙 更新时间:2023-11-03 22:18:09 25 4
gpt4 key购买 nike

我想在 btnExit_Click 中调用 btnDisconnect_Click

private void btnDisconnect_Click(object sender, EventArgs e)
{
//does something
}

private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
}

最佳答案

通常在这种情况下,我会让我的点击处理程序只调用另一个函数并传入适当的参数:

private void btnDisconnect_Click(object sender, EventArgs e)
{
DoDisconnect();
}

private void DoDisconnect()
{
...
}

然后我可以从任何地方调用相同的函数:

private void btnExit_Click(object sender, EventArgs e)
{
DoDisconnect();
}

这样,您的“断开连接”逻辑就会被采用实际上不会以任何方式影响断开连接行为的虚拟参数搞砸。

这也意味着您可以开始从表单中提取 View 逻辑。

关于c# - 我想在另一个事件中调用一个 visual c# 事件。我怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4252885/

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