如何将参数传递给事件? [英] How do I pass parameters to events?

查看:165
本文介绍了如何将参数传递给事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我如何知道参数的位置?

  public static int button21_Click(object me,EventArgs MyArgs){
button17(me,MyArts);
}


解决方案

我承认我不完全确定你在问什么,但从我可以看到,你似乎试图对多个 Button.Click 事件使用相同的事件处理程序。你可以通过两种方法来实现。



1。使用WinForms设计器。



选择其中一个按钮,然后按F4或转到属性窗口,然后选择事件选项卡(该图标的图标为闪电)。然后找到Click事件并创建一个新的处理程序。然后转到每个其他按钮,找到他们的Click事件,只需使用下拉菜单选择您已创建的处理程序。



2。使用代码



在代码中,创建一个事件处理程序,然后将该事件处理程序添加到每个按钮的单击事件。

  private void OnClick(object sender,EventArgs args)
{
//做东西来处理事件。
}

//这是表单的构造函数。
public MyWinForm()
{
InitializeComponents();

this.button1.Click + = OnClick;
this.button2.Click + = OnClick;
this.button3.Click + = OnClick;
this.button4.Click + = OnClick;
this.button5.Click + = OnClick;
this.button6.Click + = OnClick;
this.button7.Click + = OnClick;
}


Some of my variables are not getting serialized each method invocation.

How do I know where arguments go?

public static int button21_Click(object me, EventArgs MyArgs) {
  button17(me, MyArts);
}

解决方案

I admit that I am not entirely certain what you are asking, but from what I can see, you appear to be trying to use the same event handler for multiple Button.Click events. You can do this in two ways.

1. Using the WinForms designer.

Select one of your buttons then hit F4 or go to the Properties window and select the Events tab (it's the one with the icon that looks like a lightning bolt). Then find the Click event and create a new handler. Then go to each of the other buttons and find their Click event and just use the dropdown to select the handler you already created.

2. Using code.

In code, create an event handler, then add that event handler to each button's Click event.

private void OnClick(object sender, EventArgs args)
{
    // Do stuff to handle the event.
}

// This is the form's constructor.
public MyWinForm()
{
    InitializeComponents();

    this.button1.Click += OnClick;
    this.button2.Click += OnClick;
    this.button3.Click += OnClick;
    this.button4.Click += OnClick;
    this.button5.Click += OnClick;
    this.button6.Click += OnClick;
    this.button7.Click += OnClick;
}

这篇关于如何将参数传递给事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆