简体   繁体   English

访问动态创建的控件C#

[英]accessing dynamically created controls C#

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. 用户有一个他们使用的设置文件,我将其读入Windows窗体。 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 (ie if the label is "portland" the corresponding listbox is "lstportland" but these names WILL vary). 一个选项卡有2个标准列,但是那两个之后可以有更多数字,它们具有基于标签的不同标签和列表框名称(即如果标签是“portland”,则相应的列表框是“lstportland”但这些名称将变化)。 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. 在Later方法中,我需要将动态创建的每个列表框的项目读入其自己的数组中,或以某种方式访问​​列表框本身中的项目。 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);
                }

The control isn't being found because you are searching the Form's Controls() collection, instead of its actual container, tabPage4 . 找不到控件,因为您正在搜索Form的 Controls()集合,而不是它的实际容器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. 或者在评论中搜索 drzounds提供的链接中的控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM