在 Winforms 自定义 UserControl 上启用设计图面 [英] Enabling design surface on Winforms custom UserControl

查看:15
本文介绍了在 Winforms 自定义 UserControl 上启用设计图面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 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 写道:如果在设计模式下将另一个控件拖到我的用户控件(或在运行时以编程方式添加),我希望将该控件放置在面板."

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

我将把上述解释为您希望设计时拖动控件成为 UserControl 的两个内部面板之一的 子控件:如果这种解释是错误的:请忽略以下内容:)

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

另外,为了避免混淆:当您观察到在设计时放置在表单上的 UserControl 实例中的面板或其他容器"控件时,您是绝对正确的,不要消耗"或吞下"" 如您所料,拖过控件:实际上您甚至不能单独选择它们:它们被添加到 UserControl 的 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.

幸运的是,在设计时拖放案例中,有一个很好的可靠代码示例,您可以在 Henry Minute 的 CodeProject 上学习和使用:设计嵌套控件:该文章将向您展示如何从 ParentControlDesigner 继承,以便在设计时子控件是 UserControl 的容器-time 可以按照您所寻找的方式充当容器.

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.

如果您希望在运行时使用您的控件(程序员)......假设他们没有源代码,因此他们与您的 UserControl 交互作为黑匣子",能够看到"只有属性、方法、可用事件等,您已将其设为 Public ... 以控制添加的控件的放置位置:您可以决定您希望消费者如何访问面板.您可以通过 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. 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.

这篇关于在 Winforms 自定义 UserControl 上启用设计图面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆