[英]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.