【问题标题】:UserControl extending ScrollableControl - disable container functinalityUserControl 扩展 ScrollableControl - 禁用容器功能
【发布时间】:2017-04-05 11:42:56
【问题描述】:

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

我的问题是如何在扩展 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);
    }
}

【问题讨论】:

  • 也许,您可以尝试将Drop Event处理为空Container属性或清除目标控件中的子项。
  • @BaranovskiyDmitry 我想在设计时禁用它。 Drop Event 将在运行时起作用。

标签: c# .net winforms user-controls


【解决方案1】:

您在设计时通过 [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...
}

【讨论】:

    【解决方案2】:

    可能有不止一种方法可以做到这一点,但这是我要做的......

    首先创建一个只读版本的ControlCollection

    public class ReadOnlyControlCollection : Control.ControlCollection
    {
        public ReadOnlyControlCollection(Control owner)
            : base(owner)
        {
        }
    
        public override bool IsReadOnly
        {
            get { return true; }
        }
    
        public override void Add(Control control)
        {
            throw new ArgumentException("control");
        }
    }
    

    然后让你的ScrollableControlBasedControl 创建一个ReadOnlyControlCollection 的实例,而不是默认的ControlCollection

    public class ScrollableControlBasedControl : ScrollableControl
    {
        protected override Control.ControlCollection CreateControlsInstance()
        {
            return new ReadOnlyControlCollection(this);
        }
    
        // The rest of your class goes here...
    }
    

    我使用 Visual Studio 2010,当我在 ScrollableControlBasedControl 上放置一个控件时,该控件会神奇地移回原来的位置,就好像操作被取消了一样。

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 2012-11-30
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2021-11-06
      相关资源
      最近更新 更多