Problem With My WinForms CheckBox Code
I have a requirement where if i click on Checkboxes, then that corresponding port should be updated and these updated ports will be used in another file for sending ou开发者_StackOverflowtputs to these particular ports. So when i click on clickbox 1 4 and 7, then those corresponding ports should be enabled. In the place where the output has to happen(ie., class2), i run a loop, which will read from the port in a while loop(say), so only 1 4 and 7 ports which are enabled should run. The prob is, if i click on checkboxes 1 2 3, ie., consecutive numbers, it works fine, if i click on say 7, then the loop decrements and finally ports from 1 to 7 are enabled when only 7 is supposed to blink. ie., starts from 7, then 6, then 5 then finally 1 and all lights blink when only 7 is supposed to blink.
this is the checkbox condition:
private void checkBox4_CheckedChanged_1(object sender, EventArgs e)
{
if (sender is CheckBox)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox.Checked)
{
Enableports[4] = true; or Enableport(4); // im setting that port 4 to true( Enableports[4] = true ) and directly entering the value in another API( Enableport(4) ).
}
else
{
Disableport(4);
}
}
}
this is the loop :(this is in another class) bool[] Enabledports { get; set; } is the declaration for Enabled ports. void Enableport(int output); for a single one.
for (int i = 0; i <= 12; i++)
{
if (Enabledports[i] == true) // API to check those enabled ports only
{
Enableport(i);
}
}
here Enabledports is bool[]
return type.
I'm not sure if I'm doing it the right way. I'm just a beginner and any help will be highly appreciated.
I'm not completely sure that I've understood your question correctly, but I'll offer my best advice anyhow:
I think it is a bad idea to do "EnablePort()" in one place (the "loop in another class") and the "DisablePort()" in another (inside the "else" clause of the checkBox4_CheckedChanged_1() event handling method).
I don't understand why you have to do the "EnablePort()" inside a loop instead of calling it directly from the checkBox4_CheckedChanged_1() method, If not specifically required, I would suggest just calling "EnablePort()" directly.
Just my 2 cents...
精彩评论