简体   繁体   English

通过Onclick事件传递参数

[英]Pass paramater with Onclick Event

I'm wondering how to pass custom vars with a onclick event. 我想知道如何通过onclick事件传递自定义变量。

I have several buttons in a form. 我在表单中有几个按钮。 Any of this button opens a dialog, including some checkboxes. 任何此按钮都会打开一个对话框,其中包括一些复选框。 When you click on the OK button, I have to check what checkboxes have been checked and do someting else (hope you understand what I want). 当您单击“确定”按钮时,我必须检查已选中的复选框并执行其他操作(希望您了解我想要的内容)。 The dialog and any stuff are created programmatically. 对话框和任何内容都是以编程方式创建的。

So I create the checkboxes and the OK button: 所以我创建了复选框和确定按钮:

CheckBox[] chkModules = new CheckBox[lstFailedSteps.Count];
int chkCounter = 0;
foreach (int intFailedStep in lstFailedSteps) 
{
    chkModules[chkCounter] = new CheckBox();
    chkModules[chkCounter].Text = dicStepDescriptions[intFailedStep];
    chkModules[chkCounter].AutoSize = true;
    chkCounter++;
}

chkCounter = 0;
foreach (CheckBox check in chkModules)
{
    chkCounter += 25;
    check.Checked = true;
    pnlRestart.Controls.Add(check);                
}

Button btnOkay = new Button();
btnOkay.Text = "OK";
btnOkay.Click += btnOkay_Click;

Okay, and now I want to the the method, that is called when clicking on the button, pass the chkModules array. 好的,现在我想要点击按钮时调用的方法,传递chkModules数组。

Thx in advance! Thx提前! Cheers Alex 干杯亚历克斯

The fastes way is creating anonymous event for your button click: 迷人的方式是为您的按钮点击创建匿名事件:

btnOkay.Click += (sender, e) =>
{
   //implement your click logic here or call method that accepts CheckBox[] as parameter
};

this way all local variables are in scope of event. 这样所有局部变量都在事件范围内。

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

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