简体   繁体   English

在Winforms自定义UserControl上启用设计图面

[英]Enabling design surface on Winforms custom UserControl

I tried to create a custom user control in C# that handles other controls that are added to it. 我试图在C#中创建一个自定义用户控件,以处理添加到它的其他控件。 The custom control consists of two panels. 定制控件包含两个面板。 What I'm trying to achieve is, that if another control is dragged to my user control in design mode (or added programmatically at runtime) I want that control to be placed on one of the panels. 我要实现的目标是,如果在设计模式下将另一个控件拖到我的用户控件上(或在运行时以编程方式添加),我希望将该控件放在其中一个面板上。

I tried to handle the OnControlAdded event but that didn't do the trick... 我试图处理OnControlAdded事件,但没有成功。

Markus wrote : "if another control is dragged to my user control in design mode (or added programmatically at runtime) I want that control to be placed on one of the panels." Markus写道:“如果将另一个控件以设计模式拖到我的用户控件中(或在运行时以编程方式添加到我的用户控件中),我希望将该控件放置在其中一个面板上。”

I am going to interpret the above as meaning you want the Design-Time dragged control to become a child control of one of the two internal Panels of your UserControl : if that intrepretation is wrong : please disregard what follows :) 我将解释上述的意思是你想要的设计时拖着控制成为你的用户控件的两个内部面板之一的子控件 :如果intrepretation是错误的:请忽略接下来:)

Also, just to avoid confusion : you are absolutely correct when you observe that Panels, or other "container" Controls, in an instance of a UserControl placed on a Form at Design-Time, do not "consume" or "swallow" dragged over controls as you might expect : in fact you can't even select them individually : they are added to the UserControl's ControlCollection. 另外,为避免混淆:当您观察到面板或其他“容器”控件在设计时放置在窗体上的UserControl实例中,没有“消耗”或“吞咽”时,您是绝对正确的正如您所期望的那样:实际上,您甚至都不能单独选择它们:它们已添加到UserControl的ControlCollection中。

Fortunately for you, in the design-time drag-drop case there is a good solid code example you can study and use on CodeProject by Henry Minute : Designing Nested Controls : that article will show you how to inherit from ParentControlDesigner so that child controls which are containers of a UserControl at design-time can function as containers in the way you are looking for. 幸运的是,在设计时拖放情况下,有你可以学习和亨利分钟CodeProject上使用了良好稳固的代码示例: 设计嵌套控件 :这篇文章将告诉你如何从ParentControlDesigner继承,这样子控件在设计时是UserControl的容器 ,可以按照您想要的方式充当容器。

In the case of your wanting the consumer of your control at run-time (programmer) ... assuming they don't have source, so they interact with your UserControl as a "black box," able to "see" only Properties, and Methods, available Events, etc., you've made Public ... to control where an added Control is placed : you have a decision to make about how you wish the consumer to access the Panels. 如果您想在运行时使用控件的使用者(程序员)...假设他们没有源,那么他们与您的UserControl交互为“黑匣子”,只能“看到”属性,和方法,可用的事件等,您已将Public设置为控制添加控件的位置:您可以决定希望消费者如何访问面板。 You could expose them "directly" as objects, via Public Properties of the UserControl, or you could expose only a Public method for adding controls for each panel. 您可以通过UserControl的“公共属性”将它们“直接”作为对象公开,也可以仅公开用于为每个面板添加控件的Public方法。

为什么不直接将其拖到面板中,或者为其中一个面板提供公共访问权限,然后将所有编程方式直接添加到该面板中?

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

相关问题 WinForms UserControl设计 - WinForms UserControl design 用于 WinForms 设计图面上控件的 BeginResize/EndResize 事件 - BeginResize/EndResize Event for Control on WinForms Design Surface WinForms 中自定义的 UserControl 属性绑定 - custom-made UserControl property binding in WinForms 在 WinForms UserControl 中设计时显示弹出窗口的字体大小问题 - Font Size issue with a popup shown at design-time in a WinForms UserControl 如何在XAML中为WPF中托管的Winforms UserControl解析自定义属性名称? - How To Resolve Custom Property Name In XAML For a Winforms UserControl Hosted In WPF? 如何在自定义 UserControl(.Net 4、Winforms)上使用项目集合编辑器? - How to use an Items Collection Editor on a custom UserControl (.Net 4, Winforms)? 求Winforms System.ComponentModel.Design设计Surface BeginDrag / EndDrag事件挂钩 - Seeking a Winforms System.ComponentModel.Design Design Surface BeginDrag/EndDrag Event Hook 将自定义设计时命令添加到WinForms控件 - Add custom design time command to WinForms control 有关自定义控件列表的WinForms UI设计建议? - WinForms UI design advice for list of custom controls? 在C#WinForms中设计自定义TabPage? - Design Custom TabPage in C# WinForms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM