在Winform C#.net应用程序中更改动态添加的控件的值,以及如何跟踪动态添加的控件 [英] Change values of dynamically added controls in winform c#.net application, how to track dynamically added controls
问题描述
我在运行时动态地在tableLayoutpanel的行内添加了控件,添加的控件是标签,LINKLABEL和图片框。
I have added controls dynamically on runtime inside the rows of a tableLayoutpanel , the controls added are LABELS, LINKLABEL AND A PICTURE BOX.
现在,我想在单击按钮时将这些动态添加的控件(标签,链接标签)的值(文本属性)更改为某些指定值。
Now , i want to change the value(Text Property) of these dynamically added controls(Labels, Linklabels) to some specified value, on a button click.
我该怎么做?请帮助提供代码。
How do i do this? Please help with code.
这些动态控件是否具有某种ID,例如HTML中的ID。
Is there some kind of ID for these dynamically controls like we have in HTML.
此外,我正在尝试使用此功能,但徒劳无功...........
Also , am trying to use this but all in vain...........
Control[] GettableLayoutPanelControls = new Control[11];
GettableLayoutPanelControls = tableLayoutPanel1.Controls.Find("Control Name", true) ;
GettableLayoutPanelControls.SetValue("CHANGED VALUE ", 0); //this line gives error..........
推荐答案
尝试类似的操作,这将添加11个新的文本框(或您想要的任何其他控件):
Try something like this, which will add 11 new text boxes (or any other control you want):
int NumberOfTextBoxes = 11;
TextBox[] DynamicTextBoxes = new TextBox[NumberOfTextBoxes];
int ndx = 0;
while (ndx < NumberOfTextBoxes)
{
DynamicTextBoxes[ndx] = new TextBox();
DynamicTextBoxes[ndx].Name = "TextBox" + ndx.ToString();
// You can set TextBox value here:
// DynamicTextBoxes[ndx].Text = "My Value";
tableLayoutPanel1.Controls.Add(DynamicTextBoxes[ndx]);
ndx++;
}
这将动态地将文本框添加到TableLayout控件中。如果以后需要修改它们,则:
This will dynamically add Text Boxes to your TableLayout control. If you need to retreive them later:
foreach (Control c in TableLayoutPanel1.Controls)
{
if (c is TextBox)
{
TextBox TextBoxControl = (TextBox)c;
// This will modify the value of the 3rd text box we added
if (TextBoxControl.Name.Equals("TextBox3"))
TextBoxControl.Text = "My Value";
}
}
这篇关于在Winform C#.net应用程序中更改动态添加的控件的值,以及如何跟踪动态添加的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!