简体   繁体   English

从Toolbox拖动控件时,ControlDesigner不活动

[英]ControlDesigner not active when dragging control from Toolbox

I'm trying to add designer support for my custom WinForms control, but it looks designer is only active when I already have instance created, not during drag'n'drop from Toolbox. 我正在尝试为我的自定义WinForms控件添加设计器支持,但它看起来设计器只在我已经创建实例时才有效,而不是在从Toolbox拖放时。

To show what I mean I've created simple control with designer: 为了表明我的意思我已经用设计师创建了简单的控件:

[Designer(typeof(MyButtonDesigner))]
public class MyButton:Button
{
    public MyButton()
    {
        base.Size= new Size(50,50);
    }
}
class MyButtonDesigner : ControlDesigner
{
    public override bool CanBeParentedTo(IDesigner parentDesigner)
    {
        return parentDesigner != null && parentDesigner.Component is Form;
    }
}

I'd like my control to by hosted only by form (user will be able to add control only to form, not to groupbox). 我希望我的控件只能由表单托管(用户只能将控件添加到表单,而不是组合框)。 When I drag control from Toolbox my validation logic is skipped, but when I try to move my control instance from form to groupbox I can see that drop is validated (as shown below) 当我从Toolbox拖动控件时,我的验证逻辑被跳过,但是当我尝试将控件实例从表单移动到groupbox时,我可以看到drop已经过验证(如下所示)

在此输入图像描述

I'm quite new to ControlDesigner so I'm not sure if this behavior is by design or can I change this so that my validation will work when dragging from Toolbox. 我对ControlDesigner很陌生,所以我不确定这种行为是设计还是可以更改,以便从Toolbox拖动时我的验证工作正常。

I'm using Visual Studio 2013, but I don't think this should be an issue. 我正在使用Visual Studio 2013,但我不认为这应该是一个问题。

CanBeParentedTo method is not called when an item is dragged from the Toolbox onto the design surface. 将项目从“工具箱”拖动到设计图面上时,不会调用CanBeParentedTo方法。
You can create a custom ToolBoxItem for your control and then override CreateComponentsCore method to prevent creating control when the parent is not Form . 您可以为控件创建自定义ToolBoxItem ,然后覆盖CreateComponentsCore方法以防止在父级不是Form时创建控件。 The ToolBoxItem will be used when dragging control from ToolBox and the Designer will be used when dragging control from design-surface. 从ToolBox拖动控件时将使用ToolBoxItem ,并且在从design-surface拖动控件时将使用Designer

//Add reference to  System.Drawing.dll then using
using System.Drawing.Design;
using System.Windows.Forms.Design;
public class MyButtonToolBoxItem:ToolboxItem
{
    protected override IComponent[] CreateComponentsCore(IDesignerHost host,
        System.Collections.IDictionary defaultValues)
    {
        if(defaultValues.Contains("Parent"))
        {
            var parent = defaultValues["Parent"] as Control;
            if(parent!=null && parent is Form)
                return base.CreateComponentsCore(host, defaultValues);
        }

        var svc = (IUIService)host.GetService(typeof(IUIService));
        if (svc != null) svc.ShowError("Can not host MyButton in this control");

        return null;
    }
}

To register the toolbox item for your control: 要为您的控件注册工具箱项:

[ToolboxItem(typeof(MyButtonToolBoxItem))]
[Designer(typeof(MyButtonDesigner))]
public class MyButton : Button

As result, when you drag your control from toolbox and drop it on any container other than form, it shows a message to the user and nothing happens and it will not added to that parent. 因此,当您从工具箱中拖动控件并将其放在除表单之外的任何容器上时,它会向用户显示一条消息,但没有任何反应,并且不会将其添加到该父级。 If you like, you can remove the messagebox. 如果您愿意,可以删除消息框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF-VS2019 从工具箱拖动到设计视图时,如何自定义控件以使其具有默认值? - WPF-VS2019 How do I customize a control to have default values when dragging from the toolbox into design view? 工具箱中缺少用户控件 - User control missing from Toolbox 从工具箱使用时,如何使自定义控件覆盖所有必需的文件 - How to have a custom control pull over all required files when used from Toolbox 从Winforms工具箱添加控件会引发错误 - Adding control from winforms toolbox throws error 从工具箱中进行拖放控制不起作用 - Drag and Drop control from toolbox not working C#用户控件与自定义ControlDesigner的每个副本 - C# User control with custom ControlDesigner each copy 从工具箱拖放自定义控件时生成代码或事件 - Generate code or events when drag-drop a custom control from the toolbox 当自定义控件从工具箱拖放到Mainwindow时,更改XAML自动生成文本 - Change the XAML auto generating text when a custom control is dropped from the toolbox onto Mainwindow C#:使用ControlDesigner的自定义控件,在其他具有客户端配置文件的项目中使用 - C#: Custom Control with ControlDesigner, using in other Project with Client Profile 当控件在同一项目中时,如何将扩展控件添加到工具箱? - How to add an extended control to the toolbox, when the control is in the same project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM