1

I have been reading for a bit and found some things close, but not anything works in my case. The user has a settings file that they use and I read it into the Windows form. One tab has 2 standard columns but then there can be any number more after those two which have different labels and listbox names that are based on the label (i.e. if the label is "portland" the corresponding listbox is "lstportland" but these names WILL vary). There are created in the is part of the settings file import method, on the fly:

 for (int i = 3; i < (lastColumn); i++)
        {
            //creates new list box and names it the column name with a "lst" infront of it
            var cellVal = squidSheet.Cells[1, i].Value;
            string convertString = cellVal.ToString();
            string listBoxName = "lst" + convertString;
            int lbLocation = new int();

            /*where to place the next label/listbox on the sheet based on a placement point  if its the first one
             *it is placed in a specific spot, then each subsequent one is placed equidistant from the last.*/
            if (i==3)
            { lbLocation = 382; }
            else
            { lbLocation = 382 + (115*(i-3)); }                

            //create the properties for the new listbox and label in its proper place on the "Run Tab"
            ListBox listBox1 = new ListBox();
            Label label1 = new Label();
            listBox1.Name = listBoxName;
            listBox1.Height = 316;
            listBox1.Width = 94;
            listBox1.Location = new Point(lbLocation, 30);

            label1.Location = new Point(lbLocation, 14);
            label1.Text = convertString;
            label1.Name = "lbl" + convertString;
            label1.Font = new Font("Microsoft Sans Serif", 8, FontStyle.Regular); 

            //add the new listbox and label to the Form
            tabPage4.Controls.Add(listBox1);
            tabPage4.Controls.Add(label1);

            //fill the new list box
            string colIdString = TestCase((i-1).ToString());
            fillListBox(listBox1, lastRowRunList, squidSheet, colIdString);
        }

In a Later method, I need to read the items of each listbox created on the fly, into its own array or somehow access the items in the listbox itself. I have sketched the following but it doesn't work. Any ideas?

for (int l = 2; l < (listBoxNames.Count); l++)
                {                        
                    string variableNameString = labelText[l].ToString();
                    string variableNames = "#" + variableNameString + "#";
                    ListBox listboxTest = Controls[("lst" + variableNameString)] as ListBox;
                    string variableValues = listboxTest.Items[(l-1)].ToString();
                    readText = readText.Replace(variableNames, variableValues);
                }
Darw1n34
  • 312
  • 7
  • 24
  • 1
    Your code brings memories of very distant, very dark times. My recommendation: put an `ElementHost` and do this in WPF with an `ItemsControl` and save yourself from a lot of pain. – Federico Berasategui Jul 22 '15 at 18:53
  • @HighCore Thank you. Its been very frustrating, I am not familiar with the commands you talked about, but I will do my research and keep looking. Thanks – Darw1n34 Jul 22 '15 at 19:00
  • 1
    Could you save the control names in a class variable and then use Control.ControlCollection.Find like its being used here: http://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls – drzounds Jul 22 '15 at 19:03

1 Answers1

3

The control isn't being found because you are searching the Form's Controls() collection, instead of its actual container, tabPage4.

Change:

ListBox listboxTest = Controls[("lst" + variableNameString)] as ListBox;

To:

ListBox listboxTest = tabPage4.Controls[("lst" + variableNameString)] as ListBox;

Or search for the control as in the link provided by drzounds in the comments.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • that worked like a charm, can't believe I was one qualifier away from an answer...UGH...in hind site it is totally obvious. – Darw1n34 Jul 22 '15 at 19:25