Reputation: 9064
I have a tableLayoutPanel inside which i have added Panels dynamically--- the name of Panels are :
Panel1 , Panel2 . . . . . . . . . . . Panel10
Now , in these dynamically added Panels , am adding some more controls dynamically such as :
In Panel1 : LabelDate1, LabelTime1, LabelPicAdder1, LinkLabel1
In Panel2 : LabelDate2, LabelTime2, LabelPicAdder2, LinkLabel2......
Now , on a button click, i want to change the values of the controls inside the Panel1, Panel2, but if i do this :
foreach ( Control ctrl in this.tableLayoutPanel )
, i am ONLY able to get the Panels into ctrl variable,
but how to get the controls inside these Panels into some variable and change the TEXT property of those controls.....
I mean, how do i get the controls which are inside the Panels, which are themselves inside the tableLayoutPanel
TableLayoutPanel---->Dynamic Panels----------->Dynamic Controls --how to change the text property of this last object in the hierarchy
Upvotes: 0
Views: 2858
Reputation: 4683
create a Recursive function like this to read all control in a parent and child control
public void GetAllControl(Control parent)
{
//Dosomething with parent like setting text or blah blah blah
foreach (Control item in parent.Controls)
{
GetAllControl(parent);
}
}
and call this loop every where you want
foreach ( Control ctrl in this.tableLayoutPanel )
{
GetAllControl(ctrl );
}
Upvotes: 1
Reputation: 941347
You added the control to the panels. So you have to iterate each panel:
foreach ( Control panel in this.tableLayoutPanel ) {
foreach ( Control ctrl in panel) {
// etc..
}
}
Odds are good that you can simplify your code by storing the control references when you create them instead of trying to find them back later.
Upvotes: 1