我有一个WinForms程序,该程序在启动时设置了TableLayoutPanel。我向其中添加了另一组TLP,并设置了一个标签,该标签与我要绘制的颜色相对应:
private void Set_IO_Display()
{
// this sets up the TLP at run time. this all works fine. The TLPs set up here are referred to in `Update_GUI_Inputs()`
string Type; // these are all defined from an XML file
string Device = "ED527"; //these are examples
string Line = "1";//example
string label = "Test"; //example
string Colour = "Green"; //example
TableLayoutPanel IOTable = new TableLayoutPanel();
IOTable.ColumnCount = 6;
IOTable.RowCount = 1;
IOTable.Name = Device + "_" + Line;
#region AddLabels
IOTable.Controls.Add(new Label
{
Text = label,
TextAlign = ContentAlignment.MiddleCenter,
BorderStyle = BorderStyle.FixedSingle,
BackColor = Color.White,
Dock = DockStyle.Fill
}); // I add another 5 labels here
IOTable.Dock = DockStyle.Fill;
IOTable.Tag = Colour;
}
private void UpdateGUI_Inputs()
{
//this is this sub for the updates, which works okay
string I_Card_Name = "ED527_" + i.ToString();
TLP = TLP_IO_Info.Controls.Find(I_Card_Name, true).FirstOrDefault() as TableLayoutPanel;// <-- this works fine - the name given in Set_IO_Display() is found and I can do something with it.
lbl = TLP.GetControlFromPosition(4, 0) as Label;// <<-- this also works fine.
lbl.Invoke((MethodInvoker)delegate { lbl.BackColor = Color.FromName(TLP.Tag); }); //<-- the TLP tag is returned as null - Why?
}
即使找到了TableLayoutPanel
及其子标签,并且我可以对它们进行处理,但调用时它不会返回该标签。
如何获取标签?