Click here to Skip to main content
15,885,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
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.



C#
tableLayoutPanel1.SuspendLayout();
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
tableLayoutPanel1.ResumeLayout();
Posted
Comments
Sergey Alexandrovich Kryukov 17-Mar-13 1:12am    
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.)
—SA
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

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;
}
else
{
    pnl.Location = new Point(0, nextloc);
}
nextloc = nextloc + heightofGP + 5;


pnlRunner.Controls.Add(pnl);
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900