第一次放置在窗体上时如何获得WinForms自定义控件的默认值

编程入门 行业动态 更新时间:2024-10-26 18:23:17
本文介绍了第一次放置在窗体上时如何获得WinForms自定义控件的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有自定义控件的类库:

I have a class library with a custom control in it:

using System.ComponentModel; using System.Windows.Forms; namespace ClassLibrary1 { public sealed class CustomLabel : Label { [DefaultValue(false), Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public override bool AutoSize { get => base.AutoSize; set => base.AutoSize = value; } public CustomLabel() { AutoSize = false; } } }

请注意,重写方法上的构造函数和中的AutoSize都设置为false.

Notice that AutoSize is set to false in both the constructor and the designer attribute on the overridden method.

我有一个Winforms项目,我想在其中使用控件.我从工具箱中拖放了它,但是它没有将AutoSize设置为false:

I have a winforms project where I want to use the control. I drag/drop it from the toolbox, but it doesn't have AutoSize set to false:

如果我保存并关闭该表单,然后重新打开它,则现在已正确设置:

If I save and close the form and then re-open it, now it's set correctly:

第一次放置在表单上时,如何使它尊重属性值?

How can I make it respect the property value when first dropped on the form?

推荐答案

通常会尊重在构造函数中分配的默认值.但是在某些情况下,默认值将使用设计器进行更改,例如,控件的ToolboxItem的CreateComponentsCore方法.

The default values which you assign in constructor are respected in general. But for some cases the default values will be changed using designer, for example by the CreateComponentsCore method of ToolboxItem of the control.

Label的AutoSize属性的默认值为false,您甚至不需要重写它或在构造函数中进行设置. 但是,一个AutoSizeToolboxItem已分配给Label,当您在设计器中放置Label的实例时,会将AutoSize设置为true.要删除此行为,只需为您的控件分配一个新的ToolboxItem:

The default value for AutoSize property for Label is false and you even don't need to override it or set it in constructor. But an AutoSizeToolboxItem has been assigned to Label which sets AutoSize to true when you drop an instance of Label on designer. To remove this behavior, it's enough to assign a new ToolboxItemto your control:

using System.ComponentModel; using System.Drawing.Design; using System.Windows.Forms; namespace ClassLibrary1 { [ToolboxItem(typeof(ToolboxItem))] public sealed class CustomLabel : Label { } }

注释1:仅供参考,ToolboxItem具有CreateComponentsCore方法,当您将控制放到设计图面上时,可以将其用于某些初始化任务.

Note 1: Just for your information, the ToolboxItem has a CreateComponentsCore method which you can use it to to some initialization tasks when dropping control on design surface.

注释2 我也应该补充一点,当您将组件从工具箱拖放到设计图面时,CreateComponentCore方法将运行.它描述了为什么将其放到窗体上后会自动调整大小,因为它是在构造函数之后由CreateComponentCore设置的.但是,这次再次打开表单之后,将只运行构造函数并将属性设置为false.

Note 2 I should also add, the CreateComponentCore method will just run when you drop the component from toolbox to design surface. It describes why after dropping it on form, it's auto-size, because it's set by CreateComponentCore after your constructor. But after you open the form again, this time, just your constructor will run and set the property to false.

更多推荐

第一次放置在窗体上时如何获得WinForms自定义控件的默认值

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

发布评论

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

>www.elefans.com

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