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.
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..........
The most straightforward way to do this is to keep track of the dynamically created controls in a private field.
Remember, Windows Forms is a stateful environment, unlike ASP.NET, so fields don't lose their values when the user interacts with the form.
ETA:
This code from your comment adds the SAME label 5 times. I don't think that's your intent. When you set the Text property they will all have the same text because they reference the same control. You can use my solution and declare
If you have so many that you're declaring them in a loop then I would store them in a
Dictionary<string, Label>
so that you don't have to search the array to find the correct one.Try something like this, which will add 11 new text boxes (or any other control you want):
This will dynamically add Text Boxes to your TableLayout control. If you need to retreive them later: