在设计时获取当前窗体的所有控件
我有一个关于设计时的问题:
我制作了一个具有属性“链接”的组件。 这些链接是控件。现在我想制作一个 UI 对话框(用于在属性网格中编辑此属性)。
如何获取当前窗体的所有控件?我认为该组件与其有联系,但在哪里呢?我找不到任何东西。
谢谢 :)
I have a question about design-time things:
I've made a component with an property "Links".
Those links are Controls. Now I want to make a UI-Dialog (for editing this property in the property grid).
How can I get all controls of the current form? I think the component has an connection to it, but where? I can't find anything.
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要获取当前表单的所有控件,请使用以下代码获取该表单上所有控件的集合:
MyForm.Controls
查看此 MSDN 帮助
编辑:
也许这些会有帮助?
设计时编辑器对控件集合的支持
http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/64df27e7-8502-42ac-8634-cf8a8937d922/
添加对嵌套的设计时支持自定义/用户控件(Winforms)中的容器
To get all of the controls of the current form then use the following code to get a collection of all of the controls on that form:
MyForm.Controls
See this MSDN help
Edit:
Perhaps these will help?
Design-time editor support for controls collection
http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/64df27e7-8502-42ac-8634-cf8a8937d922/
Adding design-time support for a nested container in a custom/usercontrol (Winforms)
这是非常简单的事情,我不知道有任何 .NET 组件的示例可以做到这一点。您可以在设计时使用 Site 属性访问表单,但存在问题。难以处理的是用户删除已添加到控件集合中的控件。除了必须使用表单或用户控件的自定义设计器之外,我不知道有什么好的触发器可以使您的集合保持有效。
有一个更好的捕鼠器,例如,您会看到它被 HelpProvider 和 ErrorProvider 组件使用。请注意他们如何向窗体上的所有其他控件添加属性。这是通过实现 IExtenderProvider 接口来完成的。 MSDN 库文章 中有一个很好的示例。
This is quite untrivial to do, I don't know of any examples of .NET components that do this. You can get to the form at design time with the Site property but there are problems. What's hard to deal with is the user deleting controls, ones that you have already added to your controls collection. I don't know of any good trigger to keep your collection valid, beyond also having to use a custom designer for the form or user control.
There's a better mousetrap for this, you see it being used by the HelpProvider and ErrorProvider components for example. Note how they add properties to all other controls on the form. This is done by implementing the IExtenderProvider interface. There's an excellent example of this in the MSDN library article.
您可以获取 <设计时的 code>IDesignerHost 服务。该服务有一个名为
Container
的属性,其中包含Components
。然后,对于每个组件,获取INestedContainer
服务,然后从该服务获取所有组件。这就是文档大纲窗口的工作原理。我已更改其方法以使用
List
作为返回值:要过滤结果以仅包含控件,您可以调用
result.TypeOf()
。You can get
IDesignerHost
service at design-time. This service has a property calledContainer
which hasComponents
. Then for each component, getINestedContainer
service and then get all components from that service.This is how Document Outline window works. I've changed their method to use
List<IComponent>
as return value:To filter the result to contain just controls, you can call
result.TypeOf<Control>()
.不确定这是否是您想要的。
我由于不小心删除了标签控件的文本属性而“丢失”了它。
看了这里的讨论后,我终于意识到,通过在设计时访问任何控件属性,我可以使用属性窗口顶部的下拉菜单来定位控件名称。选择名称会显示该控件在窗体上的位置,并在属性编辑器中显示其属性。
Not sure if this is what you want.
I "lost" a label control by accidentally removing it's text property.
After looking here at this discussion I finally realized that by accessing ANY control property at design time I could use the drop - down at the top of the properties window to locate the control name. Selecting the name revealed the location of the control on the form and exposed it's properties in the properties editor.