c# - 扩展可滚动控件的用户控件 - 禁用容器功能

标签 c# .net winforms user-controls

我正在通过扩展 ScrollableControl 构建自定义控件。
问题是我的自定义控件充当容器 - 我可以将控件拖入其中: enter image description here

我的问题是如何在扩展 ScrollableControl

的类中禁用容器功能

下面是两个测试控件,一个扩展Control,第二个ScrollableControl

public class ControlBasedControl : Control
{
    protected override Size DefaultSize
    {
        get { return new Size(100, 100); }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.FillRectangle(Brushes.LightCoral, ClientRectangle);
    }
}

public class ScrollableControlBasedControl : ScrollableControl
{
    public ScrollableControlBasedControl()
    {
        AutoScrollMinSize = new Size(200, 200);
    }

    protected override Size DefaultSize
    {
        get { return new Size(100, 100); }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.FillRectangle(Brushes.LawnGreen, ClientRectangle);
    }
}

最佳答案

您在设计时从 [Designer] 属性获得“行为像容器”行为。从 Reference Source 复制粘贴:

[
ComVisible(true),
ClassInterface(ClassInterfaceType.AutoDispatch),
Designer("System.Windows.Forms.Design.ScrollableControlDesigner, " + AssemblyRef.SystemDesign)
]
public class ScrollableControl : Control, IArrangedElement {
   // etc...
}

完成工作的是 ScrollableControlDesigner。本身并没有做太多事情,而是派生自 ParentControlDesigner,该设计器允许控件充当子控件的父控件,并在设计时为其提供类似容器的行为。

修复很简单,你只需要使用你自己的 [Designer] 属性来选择另一个设计师。添加对 System.Design 的引用并使其看起来像这样:

using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.Design; // Add reference to System.Design

[Designer(typeof(ControlDesigner))]
public class ScrollableControlBasedControl : ScrollableControl {
    // etc...
}

关于c# - 扩展可滚动控件的用户控件 - 禁用容器功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43230202/

相关文章:

c# - 使用 Mailkit ImapClient 使用 TLS 而不是 SSL 连接到 gmail

java - 删除主键/id 引用的域实体时确保参照完整性

c# - 如何在没有自定义类类型的情况下获取一个类的属性

c# - SqlException的构造函数在哪里?

c# - 如何将大文件写入 SQL Server FILESTREAM?

c# - Windows 窗体屏幕保护程序预览窗口句柄

c# - 从原始 ECG 信号中提取心率的算法

c# - DataGridView 的绑定(bind)表仅在双击时生成一个 RowChanged 事件。我如何让它做两个?

c# - 在设计器中删除 GenerateMember 和 Modifiers 属性

C# - 使用 CSV/Excel 数据和选择特定数据的简单方法?