使用Dock.Fill的UserControl问题 [英] UserControl problem using Dock.Fill
本文介绍了使用Dock.Fill的UserControl问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我在尝试填充UserControl
时遇到问题
在面板内.
我创建了一些UserControl
.我的问题是,在尝试将它们填充到面板中时,有些正在按预期调整大小,而有些则没有反应,并保持相同的大小.
???
另一件事-正常反应的控件是PictrueBox
类型.
其他的是包含许多控件的Panel
.
thx!
hi all,
i''ve encountered a problem while trying to fill a UserControl
inside a panel.
i''ve created some UserControl
''s. my problem is that while trying to fill them in a panel, some are resizing as expected and some don''t react and remain at the same size.
???
another thing - the controls whom react normally are PictrueBox
type.
the other ones are Panel
s containing many controls.
thx!
推荐答案
这是一个旧帖子,但无论如何,我也遇到过类似的问题.
我的一组控件未正确对接(已正确放置,但是一旦我调整窗体的大小就保持相同大小(我在运行时添加了控件).因此,我实际上不得不编写一些方法来编写代码调整整个控件层次结构的大小.
Hi,
It''s an old post but anyway, I have experienced similar problems.
A set of my controls was not docking properly ( it was placed properly , but once I resized the form the controls stayed of the same size ( I was adding the controls at runtime). So , I actualy hadd to code a couple of methods to resize the whole control hierarchy.
private static void ResizeChildsInt(Control c,bool recursive) {
Rectangle r = c.DisplayRectangle;
foreach (Control item in c.Controls) {
if (item.Dock == DockStyle.Top) {
item.Top = r.Top;
item.Left = r.Left;
item.Width = r.Width;
if (item.Height > r.Height) {
item.Height = r.Height;
}
r = new Rectangle(r.X, r.Y + item.Height, r.Width, r.Height - item.Height);
} else if (item.Dock == DockStyle.Bottom) {
int bottom = r.Bottom - item.Height;
if (item.Height > r.Height) {
item.Height = r.Height;
}
item.Top = item.Height - r.Bottom;
item.Left = r.Left;
item.Width = r.Width;
r = new Rectangle(r.X, r.Y, r.Width, r.Height - item.Height);
} else if (item.Dock == DockStyle.Left) {
int width = item.Width;
if (width > r.Width) {
width = r.Width;
}
item.Top = r.Top;
item.Left = r.Left;
item.Height = r.Height;
item.Width = width;
r = new Rectangle(r.X + item.Width, r.Y, r.Width - item.Width, r.Height);
} else if (item.Dock == DockStyle.Right) {
int width = item.Width;
if (width > r.Width) {
width = r.Width;
}
item.Top = r.Top;
item.Left = r.Right - item.Width;
item.Height = r.Height;
item.Width = width;
r = new Rectangle(r.X, r.Y, r.Width - item.Width, r.Height);
}
}
//Fill
foreach (Control item in c.Controls) {
if (item.Dock == DockStyle.Fill) {
item.Top = r.Top;
item.Left = r.Left;
item.Width = r.Width;
item.Height = r.Height;
}
}
//Recursive
if (recursive) {
foreach (Control item in c.Controls) {
if (item is UserControl == false) {
ResizeChildsInt(item, recursive);
}
}
}
}
public static void ResizeChilds(Control c) {
c.SuspendLayout();
ResizeChildsInt(c, true);
c.ResumeLayout();
}
每次调整控件大小时,只需调用静态方法ResizeChilds.
Just call the static method ResizeChilds each time the control is resized.
您设置了这些面板停靠时要做什么?我建议他们按照他们的指示去做.
What have you set those panels to do when they are docked ? I''d suggest they are doing what they are told.
这篇关于使用Dock.Fill的UserControl问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文