第一次放置在窗体上时如何获得WinForms自定义控件的默认值 [英] How to get WinForms custom control's default value to be respected when first dropped on a form

查看:107
本文介绍了第一次放置在窗体上时如何获得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?

推荐答案

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

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.

LabelAutoSize属性的默认值为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自定义控件的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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