【发布时间】: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/…