在自定义的WinForms启用设计界面用户控件自定义、控件、界面、用户

2023-09-03 15:14:33 作者:疯到世界奔溃

我试图创建在C#中处理被添加到它的其他控件的自定义用户控件。自定义控制包括两个面板。我想要实现的是,如果另一个控件拖动到我的用户控制在设计模式(或添加编程方式在运行时)我想了控制,应放置在面板之一。

I tried to create a custom user control in C# that handles other controls that are added to it. 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.

我试图处理OnControlAdded事件,但没有这样的伎俩......

I tried to handle the OnControlAdded event but that didn't do the trick...

推荐答案

马库斯写道:如果另一个控件拖动到我的设计模式的用户控件(或以编程方式在运行时添加的)我想要控制放在一个面板。

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."

我要跨preT上面的意思是,你的需要的设计阶段拖到控制成为的子控件的两个内部面板之一你的用户控件的:如果intre pretation是错误的:请不要理会如下:)

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 :)

此外,只是为了避免混淆:当你观察到面板或其他的容器控件,在用户控件放置在窗体在设计时,没有消费或实例燕子,你是绝对正确的拖过控件如你所料:其实你甚至不能选择他们的个人:它们被添加到用户控件的ControlCollection

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.

幸运的是,在设计时拖放情况下,有一个良好的坚实code例如,你可以学习和亨利分在$ C $的CProject使用:的设计嵌套控件:在这篇文章将告诉你如何从ParentControlDesigner继承,这样子控件的这是容器一个用户控件在设计时的的可以作为容器在你正在寻找的方式运作。

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.

在你想要你的控制,消费者在运行时(程序员)...假设他们没有来源,所以他们与你的用户控件交互作为的情况下的黑匣子,能够看到只有属性和方法,可用的事件,等等,你所做的公共...来控制,其中添加的控件放置:你有一个决定,使你多么希望消费者访问面板。你可以直接暴露它们为对象,通过用户控件的公共属性,或者你可以添加控制每个小组只公开一个公共方法。

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. 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.