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