Click here to Skip to main content
14,666,389 members
Rate this:
Please Sign up or sign in to vote.
See more:
I've created one user control in Win-Form app.
I want to add this User Control dynamically in a panel on the form.
And this user control can be added or deleted at run-time.

My problem is that,when I add this panel in tablelayout panel at next row,it gets placed after previous row but the spacing between two row or I'd say the height of these rows are not equal.
Same with the case when I remove any of dynamically created User Control the panel should resize.

ucPanelRunner pnl = new ucPanelRunner(name, value);
pnl.AutoScroll = true;
pnl.BorderStyle = BorderStyle.None;
pnl.Name = string.Format("{0}", market.marketId);
tableLayoutPanel1.Controls.Add(pnl, 0,         tableLayoutPanel1.Controls.Count);//Add new pnl in next row
Why would require same height? For spacing, it's very usual to add an empty panel as a spacer.
(Sorry, I really failed to notice control adding line at first; so I removed my answer as redundant.)
abhijeetgupta1988 17-Mar-13 1:35am
each user control instance' height varies i.e. if I add an instance of my user control its height say 200 then new user control I add can have height of 150 and when I add this new instance tablelayout panel's row[0] height changes according to height of next row which creates the extra spacing.
and this is ruining the UI.

My requirement is that when I add new instance of User control ,it should be addded after the previous instance.
May be my choice of tablelayout panel is wrong ?
But I have little knowledge of Win-Form Apps ,so I'm stuck! :)
abhijeetgupta1988 20-Mar-13 3:01am
can any one help?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

Finally I have to do away with TableLayoutPanel and switch to Panel ;most obvious reason was that my user controls have varying height and which created messy layout.
I added my instances of user controls into panel at a location and next instances were added after previous instance.I also did the logic for recreating user controls instance in case a instance is disposed from the panel

 int heightofGP = 0;  int nextloc = 0; bool forFirst = true;//Location Offsets declared at class level

ucPanel pnl = new ucPanel();//My User Control

pnl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
pnl.AutoScroll = true;
pnl.BorderStyle = BorderStyle.None;
pnl.Name = string.Format("{0}", market.marketId);
pnl.Margin = new Padding(0, 10, 0, 0);

heightofGP = pnl.Height;
if (forFirst)
    pnl.Location = new Point(0, 0);
    forFirst = false;
    pnl.Location = new Point(0, nextloc);
nextloc = nextloc + heightofGP + 5;


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100