【问题标题】:accessing dynamically created controls C#访问动态创建的控件 C#
【发布时间】:2015-07-22 18:46:24
【问题描述】:

我已经阅读了一段时间,发现一些东西很接近,但在我的情况下没有任何效果。用户有一个他们使用的设置文件,我将其读入 Windows 表单。一个选项卡有 2 个标准列,但是在这两个具有不同标签和基于标签的列表框名称之后可以有更多的数字(即,如果标签是“portland”,则相应的列表框是“lstportland”,但这些名称将各不相同)。在设置文件的导入方法的is部分中有创建,在运行中:

 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);
        }

在稍后的方法中,我需要将动态创建的每个列表框的项目读取到其自己的数组中,或者以某种方式访问​​列表框本身中的项目。我已经绘制了以下草图,但它不起作用。有什么想法吗?

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);
                }

【问题讨论】:

  • 您的代码带来了对遥远、黑暗时代的回忆。我的建议:输入 ElementHost 并在 WPF 中使用 ItemsControl 执行此操作,这样可以避免很多痛苦。
  • @HighCore 谢谢。它非常令人沮丧,我不熟悉你所说的命令,但我会做我的研究并继续寻找。谢谢
  • 您能否将控件名称保存在类变量中,然后使用 Control.ControlCollection.Find,就像这里使用的一样:stackoverflow.com/questions/3898588/…

标签: c# controls


【解决方案1】:

找不到控件,因为您正在搜索 Form 的 Controls() 集合,而不是其实际容器 tabPage4

变化:

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

收件人:

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

或者搜索在 cmets 中 drzounds 提供的链接中的控件。

【讨论】:

  • 这就像一个魅力,不敢相信我离答案只有一个资格......呃......在后面的网站上是完全显而易见的。
猜你喜欢
  • 2011-02-28
  • 2010-12-02
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 2012-11-03
  • 1970-01-01
相关资源
最近更新 更多