简体   繁体   English

无法将Button作为参数传递

[英]Can't pass a Button as a parameter

What I'm trying to do here is really simple but it's not working for me. 我在这里尝试做的事情很简单,但对我却没有用。 I've looked up many examples of very similar or matching questions on stackoverflow and others and their solutions haven't worked for me. 我查找了许多关于stackoverflow的问题非常相似或匹配的示例,而其他示例及其解决方案对我却没有用。

Basically (in a very simplified form), I have a button with some text in it. 基本上(以非常简化的形式),我有一个带有一些文本的按钮。 I'd like it so when this button is clicked, a message box shows up displaying the text. 我想要这样,所以当单击此按钮时,会出现一个显示文本的消息框。 However, I have 9 of these buttons (think numbers on a calculator), and I'd like to create a single method that handles all of these clicks, and outputs the correct text depending on the button.. which is why I need to pass the button as a parameter. 但是,我有9个这样的按钮(在计算器上想数字),我想创建一个方法来处理所有这些单击,并根据按钮输出正确的文本。这就是为什么我需要将按钮作为参数传递。

Here's the code I have for the method that handles it so far: 这是到目前为止我处理该方法的代码:

private void btn_Click(object sender, EventArgs e, Button b)
{
    MessageBox.Show(b.Text);
}

The above small code snippet is the same solution that others have used and had working. 上面的小代码段与其他人已经使用并已起作用的解决方案相同。 However, for me this code doesn't compile and shows an error that says No overload for 'btn_Click' matches delegate 'System.EventHandler' . 但是,对我来说,此代码无法编译,并显示一条错误消息,指出“ btn_Click”没有重载会匹配委托“ System.EventHandler”

When I double click on that error, it takes me to the Designer.cs page for the form, and this is the line of code that has the error: 当我双击该错误时,它将带我进入表单的Designer.cs页面,这是出现错误的代码行:

this.btnN7.Click += new System.EventHandler(this.btnN7_Click);

I have no clue what the overload and delegate parts mean, sorry I'm pretty new to this. 我不知道重载和委托部分的含义,对不起,我对此还很陌生。 I was thinking that maybe overload has to do with constructors but even if that's correct, I'm unsure of what the next step would be. 我当时在想,也许重载与构造函数有关,但即使这是正确的,我也不确定下一步将是什么。

I have System.Windows.Forms; 我有System.Windows.Forms; included properly so the issue shouldn't be that the Button object wasn't recognized. 正确包含在内,因此问题不应该是未识别Button对象。

If you could provide any insight as to what I'm missing or doing wrong, that'd be very sweet! 如果您可以提供有关我缺少或做错什么的任何见解,那将非常好! Please let me know if you need any additional information to continue. 如果您需要任何其他信息以继续,请告诉我。

And lastly, this is unrelated to my issue but it's a small question that's been irking me ever since I started using VS a week ago: Are the control parameters object sender and EventArgs e that are automatically created for controller events even necessary? 最后,这与我的问题无关,但是自从我一周前开始使用VS以来,这一直困扰着我一个小问题:是否需要为控制器事件自动创建的控制参数对象senderEventArgs e Most of the examples I've looked up online omit them. 我在网上查找的大多数示例都将其省略。 I've just kept them in since they were created by default but I don't really know what kind of function they provide, and I've never had to use those parameters in my methods. 因为它们是默认创建的,所以我一直保留它们,但是我真的不知道它们提供的功能是什么,而且我从不需要在方法中使用这些参数。

Thanks all! 谢谢大家! =) =)

You just need to use sender parameter.You can't subscribe an event handler if the method signature doesn't match with the EventHandler delegate. 您只需要使用sender参数。如果方法签名与EventHandler委托不匹配,则无法订阅事件处理程序。

private void btn_Click(object sender, EventArgs e)
{
   var currentButton = sender as Button;
   if(currentButton != null) MessageBox.Show(currentButton.Text);
}

sender will be assigned to an object that triggers the event.So for instance when your button2 is clicked, it will be assigned to button2 .Ofcourse you need to attach this event handler to button2 's Click event.I have also used the as operator to ensure that a button is triggered the event.In the future if you call this method manually like btn_Click(this, EventArgs.Empty) then the explicit cast will throw an InvalidCastException .Using as operator is always better to avoid this.If the type of the sender is different than Button then currentButton will be null . sender将被分配给触发事件的对象。例如,当您单击button2时,它将被分配给button2当然,您需要将此事件处理程序附加到button2的Click事件。我也使用了as运算符以确保按钮触发event.In未来如果调用此方法手动像btn_Click(this, EventArgs.Empty)显式类型转换将抛出一个InvalidCastException 。采用as运营商始终是更好地避免this.If类型sender ButtonButton不同,那么currentButton将为null

which is why I need to pass the button as a parameter. 这就是为什么我需要将按钮作为参数传递的原因。

You don't really have to create that extra parameter. 您实际上不必创建该额外的参数。 What you need is already there. 您所需要的已经在那里。

object sender is a source of the event, so just call object sender是事件的来源,因此只需调用

var btn = (Button) sender;
MessageBox.Show(btn.Text);

and you should be fine. 你应该没事的

Are the control parameters object sender and EventArgs e that are automatically created for controller events even necessary? 为控制器事件自动创建的控制参数对象发送者和EventArgs e是否必要?

So now you have a part of an answer for this question. 因此,您现在可以部分回答该问题。 The second part ( EventArgs ) are needed for passing additional information. 第二部分( EventArgs )是传递附加信息所必需的。 Read more about event args on MSDN . MSDN上了解有关事件args的更多信息。

You have two options here: 您在这里有两个选择:

  • If you want to access to the button that fired the event, you can do it trough sender 如果要访问触发事件的按钮,则可以通过sender

     private void btn_Click(object sender, EventArgs e) { Button b = (Button)sender; MessageBox.Show(b.Text); } 
  • Otherwise, access directly through the button name (in case you know it) 否则,直接通过按钮名称访问(如果您知道的话)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM