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. :)
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.
Comments