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