Resolved Dynamically changing a property value while in a 'for' statement

ross_p

New member
Joined
May 18, 2020
Messages
2
Programming Experience
1-3
Hi everyone, I'm a fairly new programmer, hobbyist only and much self taught... I've got a small application which polls numbered devices over a serial connection. As it does so i would like to to update some status blocks in the UI with different background colours depending on the status. I can easily assign a background colour to the item if i know the name of the item but my item name changes every time i cycle throught the for statement.
The items i want to change the colour on are named squentially eg: lblCms1, lblCms2, lblCms3 etc. and the for loop iterates through an integer i which would relate to each one of these.
I hope what i've written makes sense. I've attached a screenshot of the application and the relevant section of code is below. It's the list of CMS1, CMS2 etc labels i'd like to change the background colour of.

C#:
        private async Task  PollNetworkAsync()
        {
            cts = new CancellationTokenSource();
            int _camOrigin = Int32.Parse(camOrigin.Text);
            int _camNumber = Int32.Parse(camNumber.Text);

           while (true)
            {
                for (int i = _camOrigin; i <= (_camOrigin + _camNumber); i++)
                {
                    toolStripStatusLabel1.Text = "Polling CMS " + i + ". "; // Attempt: " + (myRetries + 1);
                    log.AppendText("Polling CMS " + i + ". "); // Attempt: " + (myRetries + 1) + " : ");
                    char message = '?';
                    await sendSerialWaitForReply(i, message);
                }
            }
            toolStripStatusLabel1.Text = "Poll cycle complete";
            serialPort1.DiscardInBuffer();
        }

        private async Task sendSerialWaitForReply(int dest, char message)
        {
            int myRetries = 0;
            int _camRetries = Int32.Parse(camRetries.Text);
            int _camTimeout = Int32.Parse(camTimeout.Text);

            while (myRetries < _camRetries)
            {
                try
                {
                    serialPort1.WriteLine("<" + dest + ",00," + message + ">");
                    serialPort1.ReadTimeout = _camTimeout;
                    string recievedParameters = await Task.Run(() => serialPort1.ReadTo("\n"), cts.Token);
                    serialPort1.DiscardInBuffer();
                    receivedData.Text = recievedParameters;
                    await parseIncomingSerial(recievedParameters);
                    break;
                }
                catch (OperationCanceledException)
                {
                    log.AppendText("Polling Cancelled by user!" + Environment.NewLine);
                    cts = null;
                }
                catch (TimeoutException)
                {
                    toolStripStatusLabel1.Text = "Timeout";
                    log.AppendText("CMS did not reply. " + Environment.NewLine);
                    myRetries++;
                    //break;
                }
            }
        }
 

Attachments

  • comms controller screenshot.png
    comms controller screenshot.png
    37.2 KB · Views: 21
For example get the control array, then access by index:
C#:
var labels = groupBoxOverview.Controls.OfType<Label>().ToArray();
If they don't appear in the right order you could turn it into a dictionary using the number in Text, then access by key:
C#:
var labels = groupBoxOverview.Controls.OfType<Label>().ToDictionary(lbl => int.Parse(lbl.Text.Substring(3)), lbl => lbl);
 
Back
Top Bottom