dimanche 17 janvier 2021

.Add() method not working when building a list utilizing a "for" loop in C#

I'm a C# novice. I'm working on a form which presents 10 rows of “data” with each row being controlled by the state of the leading checkbox. The row layout is: checkbox + label + combobox + combobox + textbox. When a user initially accesses the form only the checkboxes are enabled. Selecting (checking) a checkbox enables the controls in that row. Selecting a lower level box “activates” all rows above it and deselecting a higher level box “deactivates” all rows (if any) below it. If rows containing user input are deselected the deselected rows “revert” to initial condition. All action regarding the form controls is triggered by changes to the checkbox state. I am able to accomplish the objectives through brute force (only showing code for one checkbox state change but the code is similar for all checkbox changes):

private void placeLevelCheckBox09_.CheckedChanged(object sender, EventArgs e)
{
    if (placeLevelCheckBox09.Checked == false)
    {
        placeLabel09.Enabled = false;
        PlaceComboBox09.Enabled = false;
        TypeComboBox09.Enabled = false;
        PlaceComboBox09.Text = "";
        TypeComboBox09.Text = "";
        initialzeTypeDisplay09();
        return;
    }
    placeLabel09.Enabled = true;
    PlaceComboBox09.Enabled = true;
    TypeComboBox09.Enabled = true;
    placeLevelCheckBox00.Checked = true;
    placeLevelCheckBox01.Checked = true;
    placeLevelCheckBox02.Checked = true;
    placeLevelCheckBox03.Checked = true;
    placeLevelCheckBox04.Checked = true;
    placeLevelCheckBox05.Checked = true;
    placeLevelCheckBox06.Checked = true;
    placeLevelCheckBox07.Checked = true;
    placeLevelCheckBox08.Checked = true;
}

My code seems clumsy and inefficient so I'm trying to 'tighten' it up using some sort of looping mechanism to generate and execute/invoke the appropriate statements.

I tried an approach suggested on a Microsoft site based on a ToList().ForEach() statement declared in the public Form() block then using tags to establish what I think of as a parent-child relationship between a row's checkbox and the other controls on that row. I was not able to get that to work.

I've now created a method to be called when the checkbox state changes (it currently only addresses a single checkbox change but I'm presuming I can get the method to address all checkboxes using nested "if" statements and/or introducing additional variables):

private List<string> flushForm00()
{
    var flushFields = new List<string>();
    flushFields.Clear();

    if (placeLevelCheckBox00.Checked == false)
    {
        for (int i = 0; i < 10; i++)
        {
            string flushEachN = $"PlaceComboBox0{i}.Text = \"\";";
            //string flushEachT = $"TypeComboBox0{i}.Text = \"\";";
            Console.WriteLine(flushEachN);
            Console.WriteLine($"TypeComboBox0{i}.Text = \"\";");
            flushFields.Add(flushEachN);
            flushFields.Add($"TypeComboBox0{i}.Text = \"\";");
        }
        for (int i = 1; i < 10; i++)
        {
            string flushEachC = $"placeLevelCheckBox0{i}.Checked = false;";
            flushFields.Add(flushEachC);
            Console.WriteLine(flushEachC);
        }
    }

The loop is iterating as intended as can be seen from Control.WriteLine output

PlaceComboBox00.Text = "";
TypeComboBox00.Text = "";
...
PlaceComboBox09.Text = "";
TypeComboBox09.Text = "";
placeLevelCheckBox01.Checked = false;
...
placeLevelCheckBox09.Checked = false;
System.Collections.Generic.List`1[System.String]

As can also be seen from the output the list "flushFields" remains empty. I've tried playing with static/void on the method declaration line with no success.

I've successfully used the .Add() method elsewhere but not in a loop. Why isn't the list being populated and is the loop code "fix-able" and if so how?

My apologies for how long this has grown to be. Thank you in advance for your time and expertise.




Aucun commentaire:

Enregistrer un commentaire