2011-01-28 102 views
1

我想从我的窗体的按钮单击事件调用一个共享的全局事件处理程序。如何将额外的参数传递给我的事件处理程序?

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! 
} 

我并不总是需要在button17_Click()方法XML,只有当按下button21。

我该怎么做?

+0

你的朋友肯定能说流利的“网络英语”。但这只是您之前问题的重复。你从昨天开始没有取得任何进展吗? – bzlm 2011-01-28 19:56:32

+1

如何按下按钮? – 2011-01-28 19:57:08

+1

@ AS-CII悲伤按钮从不被点击。 http://www.mp3-to-m4r.net/images/sad-button.png – bzlm 2011-01-28 19:58:27

回答

3

我不会尝试直接调用你的事件处理程序,将它留给事件本身。

你应该做的是将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() 
} 
4

您不会将XML(或其他任意数据类型)传递给.NET中的事件处理程序。他们有一个特殊的签名,这就是你必须使用的。

您不应该从button21_click调用button17_click。相反,试试这个:

public void button17_Click(object sender, EventArgs e) 
{ 
    CommonFunctionality(); 
} 

public void button21_Click(object sender, EventArgs e) 
{ 
    CommonFunctionality(); 
} 

private void CommonFunctionality() 
{ 
    // In here, place the code that used to be in button17_click 
} 

你可能会需要创建一个版本,可以使用XML的通用功能。

2

OK,这个问题比昨天好一点,BU我们依然无法看到:

  • 是什么button17_Click
  • 为什么button17_Click的事件处理程序的声明?
  • 为什么它是'全球'事件处理程序?
  • 这是什么意思'全球'在这里?
  • 它是你的代码(你可以改变button17_Click)?
  • ...

和一切需要形成一个答案或建议。

相关问题