DaFunkyAlex
DaFunkyAlex

Reputation: 1969

Pass paramater with Onclick Event

I'm wondering how to pass custom vars with a onclick event.

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.

Thx in advance! Cheers Alex

Upvotes: 1

Views: 93

Answers (1)

gzaxx
gzaxx

Reputation: 17590

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.

Upvotes: 3

Related Questions