如何在设计时修改usercontrol的子控件?

编程入门 行业动态 更新时间:2024-10-25 18:35:21
本文介绍了如何在设计时修改usercontrol的子控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好, 我试图制作一个自定义的usercontrol,它是从工具箱和listview中获取的,我需要通过拖动到窗体然后添加来创建此控件的实例根据设计时的情况,不同的工具箱项目。 我想点击控件中的子控件并修改其属性。 有没有办法实现这个目标? 您的回复将不胜感激。

Hello, I tried to make a custom usercontrol which is derieved from toolbox and listview, I need to create instances of this control by dragging to form then add different toolbox items according to circumstance at design time. I wanted to click onto the subcontrol in the control and modify its properties. Is there a way to achieve this? Your reply will be appreciated.

推荐答案

这不是自定义控件的工作方式,但你可以这样做 - 你需要做的就是创建一个Container控件。 MSDN可以提供帮助: support.microsoft/kb/813450 [ ^ ] That's not how a Custom control is meant to work, but you can do it - all you need to do is create a Container control. MSDN can help: support.microsoft/kb/813450[^]

我认为你有来自OriginalGriff的正确答案;如果您想在CodeProject上看到此问题的一些先前讨论: 2009年7月CP文章:Henry Minute的设计嵌套控件:[ ^ ]。 2011年7月CP提示/技巧:设计时将控件添加到WinForm上的用户窗体作者:Bill Woodruff:[ ^ ] 许多人没有意识到(在我的2011年提示中),通过在UserControl的类定义之前使用那个简单的属性,你可以启用嵌套...到任何深度...控件和其他UserControls,在修改后的UserControl。 是的,你甚至可以在修改后的UserControl中拥有修改过的UserControl本身的嵌套实例。 然而,问题出现了:你真的想利用这样的自由:使用这种类型的控件嵌套会导致良好的面向对象设计,考虑到你手头的任务,以及未来维护代码的可能问题吗? br /> 您在设计时可能已添加到表单上UserControl的实例的所有控件仍然绑定到您的应用程序正在创造:即,它们不可重复使用。 如果您希望修改后的UserControl可以重复使用:我建议您考虑(缓慢而稳定的方法?): 1.定义你的基础UserControl 2.因为需要创建基本UserControl的其他变种:创建一个新的UserControl ,并使其继承自基本UserControl。 如果你这样做,请注意: 1当然,基本UserControl上的任何控件都将出现在继承的UserControl上,但是它们将被锁定。 如果你有一个Click on UserControl1上有一个Click EventHandler,然后你子类UserControl1来创建UserControl1a:a点击UserControl1a中的按钮将触发UserControl1中的Click EventHandler。 2.如果是基本UserControl使用Attribute进行装饰,允许它在设计时作为Container运行,继承的UserControl也具有这种能力:不需要也是wri继承UserControl中的属性。 3.如您所料,您可以向继承的UserControl添加任何类型的新控件,它们不会影响以任何方式基于UserControl。 I think you have the correct answer from OriginalGriff; if you want to see some previous discussion of this issue here on CodeProject: July 2009 CP article: "Designing Nested Controls" by Henry Minute: [^]. July 2011 CP Tip/Trick: "DesignTime adding of Controls to a UserForm on a WinForm" by Bill Woodruff: [^] What many people do not realize (pointed out in my 2011 Tip) is that by use of that simple Attribute before a UserControl's Class definition you can enable nesting ... to any depth ... of Controls, and other UserControls, inside the modified UserControl. Yes, you can even have nested instances of the modified UserControl itself inside the modified UserControl. However, the question arises: do you really want to take advantage of such "freedom:" does use of this type of nesting of Controls result in good OO design, given your task at hand, and the possible issues of future maintenance of your code ? All the Controls you may have added to an instance of a UserControl on a Form at design-time remain "bound" to the application you are creating: i.e., they are not re-usable. If you want your modified UserControl to be re-usable: I propose you consider (the slow and steady approach ?): 1. define your "base" UserControl 2. as the need arises to create other variants of the base UserControl: create a new UserControl, and make it inherit from the base UserControl. If you do this, note: 1. Any Controls on the base UserControl will now, of course, appear on the inherited UserControl, but they will be locked. If you have Button on UserControl1 with a Click EventHandler, and you then sub-class UserControl1 to create UserControl1a: a Click on the Button in UserControl1a will fire the Click EventHandler in UserControl1. 2. If the base UserControl was decorated with the Attribute that allows it function as a Container at design-time, the inherited UserControl will also have that ability: no need to also write that Attribute in the inherited UserControl. 3. as you might expect, you can add new Controls of any Type to the inherited UserControl, and they do not affect the base UserControl in any way.

更多推荐

如何在设计时修改usercontrol的子控件?

本文发布于:2023-11-03 13:19:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   如何在   usercontrol

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!