views:

76

answers:

1

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 outputs 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.

A: 

Hi,

I'm not completely sure that I've understood your question correctly, but I'll offer my best advice anyhow:

  1. 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).

  2. 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...

S.C. Madsen
As for ur second point, there is some other function, which will transmit the data through the selected port which is checked using the checkbox. So for the selected number of ports, i thought im supposed to run a loop. ie., if i select 4 and 9 then 4 should also work and 9 should also work continuously. But in my case first decrement happens 9 8 7 6 ... till 1. Please correct me if im wrong.
SLp
Then I think you need to move the "enabled ports" logic into the transmitting class, so all the GUI has to worry about is to call some enable/disable routine, and possibly query a list of enabled ports. But keep the actual port-logic together in one place...
S.C. Madsen