How to assign EventHandler to dynamic object in a loop?

Orkun Uğuz

I'm trying to create an Interface for my project and i created a struct of PSU like:

public struct PSU
{
    public CheckBox CallHostessButton0, CallHostessButton1,
        ReadLightButton0, ReadLightButton1, ReadLightButton2, ReadLightButton3;
    public PictureBox BeltLight, SmokeLight;
}

public bool AssignObjectsToPSU(PSU psu, CheckBox CallHostessBtn0, CheckBox CallHostessBtn1,
CheckBox ReadLightBtn0, CheckBox ReadLightBtn1, CheckBox ReadLightBtn2, CheckBox ReadLightBtn3,
PictureBox BeltLght, PictureBox SmokeLght)
{
    try
    {
        Log("[Info] Assigning objects to PUS units...");
        psu.CallHostessButton0 = CallHostessBtn0;  
        // -> In here if possible <-
        psu.CallHostessButton1 = CallHostessBtn1;
        psu.ReadLightButton0 = ReadLightBtn0;
        psu.ReadLightButton1 = ReadLightBtn1;
        psu.ReadLightButton2 = ReadLightBtn2;
        psu.ReadLightButton3 = ReadLightBtn3;
        psu.BeltLight = BeltLght; psu.SmokeLight = SmokeLght;

        LogReport("[OK]");
        return true;
    }
    catch
    {
        LogReport("[Failed]");
        return false;
    }
}

And i need to assign object events for these objects like:

PSU TestPSU = new PSU;
TestPSU.CallHostessButton0 = checkBox1;
TestPSU.CallHostessButton0.CheckedChanged += new EventHandler(checkBox1_CheckedChanged);

So I need 12 of these "PSUs" and I have to assign them for each. Like above. Here is my question: Is there any easy way to do this assignments? Ex: In a method or loop? Something like:

public void AssignEvents(PSU p, CheckBox cb)
{
    p.CallHostessButton0.CheckedChanged += new EventHandler(cb_CheckedChanged);
}

Or inside the struct definition:

psu.CallHostessButton0 = CallHostessBtn0;
psu.CallHostessButton0.CheckedChanged = += new EventHandler(CallHostessBtn0_CheckedChanged);

Thanks for helping. :)

Olivier Jacot-Descombes

You can pass a delegate to AssignObjectsToPSU as an additional parameter

public bool AssignObjectsToPSU(PSU psu, ..., EventHandler checkedChangedHandler)
{
    ...
    psu.CheckedChanged  += checkedChangedHandler;
    ...
}

Then you can call it with

if (AssignObjectsToPSU(psu, ..., checkBox1_CheckedChanged)) {
    ...
}

Just make sure not to append braces () after checkBox1_CheckedChanged.

And in a loop

foreach (PSU psu in myPsuList) {
    if (!AssignObjectsToPSU(psu, ..., checkBox1_CheckedChanged)) {
        break;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to assign dynamic variables in a for loop in python

How can i assign an interface object to dynamic object in a method?

Jmeter - How to assign multiple dynamic values to a variables inside a loop

Assign values to object in for loop

How to create a dynamic object key/value with a loop?

How to assign different variables to a class object using for loop?

How to dynamically create an object property & assign values using a for loop

how to assign object in object

How to assign something in a for loop?

Assign value to Dynamic variable inside While Loop

How to dynamic assign type in typescript?

How to assign object to ref

How to assign object to field

How make dynamic objects names to assign to a class? / How create multiple windows with object names different Tkinter

VBA: assign dynamic array in user object definition

Dynamic object field assign by another model

How to Count the Object Properties values and Displaying the serparate values in Dynamic button and assign click event to them

How to update hashmap field by dynamic key using Object.assign and avoid mutations?

How to make a for loop dynamic?

How to set a dynamic loop

How to generate dynamic for loop

Assign an object to an array within a loop in VBA

Assign the "Asynchronous result" of http call to an object in "For loop"

Assign model object to TextBoxFor dynamically inside a For loop

Assign a value to every object in an array without loop

Object.assign not copying as expected in loop

assign multiple values to object property with for loop

How to assign a variable in inline for loop?

How to assign names in a MATLAB loop