在Win-Form应用程序中动态添加用户控件 [英] Adding User Controls Dynamically In Win-Form App

查看:101
本文介绍了在Win-Form应用程序中动态添加用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我已经在Win-Form应用程序中创建了一个用户控件。

我想在面板中动态添加此用户控件表格。

这个用户控件可以在运行时添加或删除。



我的问题是,当我添加这个在下一行的tablelayout面板中的面板,它被放置在前一行之后,但是两行之间的间距或我会说这些行的高度不相等。

与我删除时的情况相同任何动态创建的用户控件面板应该调整大小。







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.



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();

推荐答案

最后我必须取消TableLayoutPanel并切换到Panel;最明显的原因是我的用户控件有不同的高度,并创建了凌乱的布局。

我将用户控件的实例添加到一个位置的面板中,并在之前的实例之后添加了下一个实例。我还做了重新创建用户控件实例的逻辑实例从面板处理



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);


这篇关于在Win-Form应用程序中动态添加用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆