自定义嵌套容器控件

发布于 2024-09-10 10:57:34 字数 2077 浏览 1 评论 0原文

我正在编写一个自定义控件,它由嵌套在普通 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2024-09-17 10:57:34

您是否考虑过让 SlidePanel 直接从 FlowLayoutPanel 继承而不是包含一个?

Have you considered having SlidePanel inherit directly from FlowLayoutPanel rather than contain one?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文