自定义嵌套容器控件
我正在编写一个自定义控件,它由嵌套在普通 Panel
中的 FlowLayoutPanel
组成。 FlowLayoutPanel
是类的内部组件,设计者不应该查看它(与 Tab
不同,它公开了其单独的属性。)设计器添加到 Panel
的内容应该添加到 FlowLayoutPanel
中。这是我到目前为止所拥有的:
public class SlidePanel : Panel
{
private FlowLayoutPanel _panel;
public SlidePanel()
: base()
{
_panel = new FlowLayoutPanel();
Controls.Add(_panel);
_panel.Location = new Point(0, 0);
_panel.Size = base.Size;
_panel.Anchor = AnchorStyles.Bottom | AnchorStyles.Top;
ControlAdded += new ControlEventHandler(SlidePanel_ControlAdded);
}
void SlidePanel_ControlAdded(object sender, ControlEventArgs e)
{
Controls.Remove(e.Control);
_panel.Controls.Add(e.Control);
}
}
这适用于在运行时添加的控件,但是当我尝试在设计时添加控件时,它要么说 'child' 不是该父级的子控件。
或相反,将控件添加到表单中。我假设有一种更干净、更好的方法来实现这一点?
测试 Flynn1179 的建议:
public class SlideControl : FlowLayoutPanel
{
private const int SB_HORZ = 0x0;
private const int SB_VERT = 0x1;
[DllImport("user32.dll")]
private static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("user32.dll")]
private static extern int GetScrollPos(IntPtr hWnd, int nBar);
public SlideControl()
: base()
{
this.MouseMove += new MouseEventHandler(SlideControl_MouseMove);
}
void SlideControl_MouseMove(object sender, MouseEventArgs e)
{
HScrollPos = e.X;
VScrollPos = e.Y;
}
protected int HScrollPos
{
get { return GetScrollPos((IntPtr)this.Handle, SB_HORZ); }
set { SetScrollPos((IntPtr)this.Handle, SB_HORZ, value, true); }
}
protected int VScrollPos
{
get { return GetScrollPos((IntPtr)this.Handle, SB_VERT); }
set { SetScrollPos((IntPtr)this.Handle, SB_VERT, value, true); }
}
}
I'm writing a custom control which consists of a FlowLayoutPanel
nested in a normal Panel
. The FlowLayoutPanel
is internal to the class, and shouldn't be able to be viewed by the designer (unlike a Tab
, which has its individual properties exposed.) Any control the designer adds to the Panel
should instead be added to the FlowLayoutPanel
. Here's what I have so far:
public class SlidePanel : Panel
{
private FlowLayoutPanel _panel;
public SlidePanel()
: base()
{
_panel = new FlowLayoutPanel();
Controls.Add(_panel);
_panel.Location = new Point(0, 0);
_panel.Size = base.Size;
_panel.Anchor = AnchorStyles.Bottom | AnchorStyles.Top;
ControlAdded += new ControlEventHandler(SlidePanel_ControlAdded);
}
void SlidePanel_ControlAdded(object sender, ControlEventArgs e)
{
Controls.Remove(e.Control);
_panel.Controls.Add(e.Control);
}
}
This works for controls added during runtime, but when I try to add a control during design time, it either says 'child' is not a child control of this parent.
or adds the control the the form instead. I'm assuming there's a cleaner, nicer way to accomplish this?
Test of Flynn1179's suggestion:
public class SlideControl : FlowLayoutPanel
{
private const int SB_HORZ = 0x0;
private const int SB_VERT = 0x1;
[DllImport("user32.dll")]
private static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
[DllImport("user32.dll")]
private static extern int GetScrollPos(IntPtr hWnd, int nBar);
public SlideControl()
: base()
{
this.MouseMove += new MouseEventHandler(SlideControl_MouseMove);
}
void SlideControl_MouseMove(object sender, MouseEventArgs e)
{
HScrollPos = e.X;
VScrollPos = e.Y;
}
protected int HScrollPos
{
get { return GetScrollPos((IntPtr)this.Handle, SB_HORZ); }
set { SetScrollPos((IntPtr)this.Handle, SB_HORZ, value, true); }
}
protected int VScrollPos
{
get { return GetScrollPos((IntPtr)this.Handle, SB_VERT); }
set { SetScrollPos((IntPtr)this.Handle, SB_VERT, value, true); }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过让
SlidePanel
直接从FlowLayoutPanel
继承而不是包含一个?Have you considered having
SlidePanel
inherit directly fromFlowLayoutPanel
rather than contain one?