gpt4 book ai didi

c# - 如何将额外的参数传递给事件处理程序?

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:52 24 4
gpt4 key购买 nike

我试图从表单的按钮单击事件中调用共享的全局事件处理程序。

public void button21_Click(object me, EventArgs MyArgs) {
button17_Click(me, MyArgs); /// WORKS!
}


我要做的是将XML传递给该方法。

就像是:

public void button21_Click(object me, EventArgs MyArgs) {
button17_Click(me, MyArgs, MyXmlString); /// ERROR!
}


仅在按下button21时,我并不总是在button17_Click()方法中需要XML。

我怎样才能做到这一点?

最佳答案

我不会尝试直接调用您的事件处理程序,而是让事件自己处理。

相反,您应该做的是将button17的逻辑移到单独的方法中,然后调用该方法。

private void button17_Click(object sender, EventArgs e)
{
// call the newly created method instead (with the XML argument null)
HandleClickOperation(null);
}

private void button21_Click(object sender, EventArgs e)
{
// call the newly created method instead (with the XML argument set)
HandleClickOperation(MyXmlString);
}

private void HandleClickOperation(string xmlString)
{
if (xmlString == null)
{
// do things unique to button17 behavior
}
else
{
// do things unique to button21 behavior
}
// the logic that was in button17_Click()
}

关于c# - 如何将额外的参数传递给事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4832439/

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