【发布时间】:2011-03-17 22:39:33
【问题描述】:
我正在编写一个自定义控件,该控件由嵌套在普通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' is not a child control of this parent.,要么将控件添加到表单中。我假设有一种更清洁、更好的方法来实现这一点?
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); }
}
}
【问题讨论】:
标签: c# winforms custom-controls components containers