I am creating a custom Label control (by simply Inheriting the standard Label control and re-painting the background and text) because I need a very specific background and border. In the constructor of the control, I set the AutoSize property to false so I can have a standard default size for the new label.
我正在创建一个自定义Label控件(通过简单地继承标准Label控件并重新绘制背景和文本),因为我需要一个非常特定的背景和边框。在控件的构造函数中,我将AutoSize属性设置为false,因此我可以为新标签设置标准默认大小。
Public Sub New()
'Set the default size of the control to 75x24
Me.Height = 24
Me.Width = 75
'Turn off the autosize property.
Me.AutoSize = False
'Turn on double-buffering.
Me.DoubleBuffered = True
End Sub
In my application that uses this control, if I create the new custom label at run time (in code), the AutoSize property stays False, and it works properly.
在我使用此控件的应用程序中,如果我在运行时(在代码中)创建新的自定义标签,则AutoSize属性保持为False,并且它可以正常工作。
If I try to add the new custom label to my form at design time, it comes in with the AutoSize property set to True, and I have to manually set it to False in the properties window. It's not a huge problem, but I don't understand why the behavior is different.
如果我尝试在设计时将新的自定义标签添加到我的表单中,则将AutoSize属性设置为True,我必须在属性窗口中手动将其设置为False。这不是一个大问题,但我不明白为什么行为不同。
Any ideas what is causing this difference in behavior?
是什么导致了这种行为上的差异?
4 个解决方案
#1
In your label class, you should override the AutoSize property.
在标签类中,您应该覆盖AutoSize属性。
//(In C#)
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
public new bool AutoSize
{
get { return base.AutoSize; }
set { base.AutoSize = value; }
}
The browsable(false) will hide the property at design time and the DesignerSerializationVisibility attribute will tell the designer to not write any code into your designer file.
可浏览(false)将在设计时隐藏属性,DesignerSerializationVisibility属性将告诉设计者不要将任何代码写入设计器文件。
#2
I finally got this to work in VB. I had to disable the Set statement, essentially turning the Overridden AutoSize Property into a read-only property.
我终于让它在VB中工作了。我必须禁用Set语句,实际上将Overridden AutoSize属性转换为只读属性。
Public Overrides Property AutoSize() As Boolean
Get
Return MyBase.AutoSize
End Get
Set(ByVal value As Boolean)
'Do nothing here
End Set
End Property
Thanks to NascarEd for getting me pointed in the right direction.
感谢NascarEd让我指出了正确的方向。
#3
Just for your future info, to set the autosize property to False in the properties window, you need to set an attribute:-
仅为了将来的信息,要在属性窗口中将autosize属性设置为False,您需要设置一个属性: -
<System.ComponentModel.DefaultValue(False)> _
Public Overrides Property AutoSize() As Boolean ....
Public Overrides Property AutoSize()As Boolean ....
#4
If you go into the design mode for the new control you are creating, you should be able to select that control and change the properties however you'd like. From that point on, whenever you add that control to form (or another control) it will have the properties you set there as the default. This should allow you to set defaults while also keeping them visible so that developers can change things should they not want it to be resizeable in the future.
如果您进入正在创建的新控件的设计模式,您应该能够选择该控件并根据需要更改属性。从那时起,无论何时将该控件添加到表单(或其他控件),它都会将您在此处设置的属性设置为默认值。这应该允许您设置默认值,同时保持它们可见,这样开发人员可以在不希望将来可以调整大小的情况下进行更改。
Alternatively, check out the code generated by the designer, as it will show you exactly what it did to generate the behavior that you're looking for.
或者,查看设计器生成的代码,因为它将准确显示它为生成您正在寻找的行为所做的工作。
#1
In your label class, you should override the AutoSize property.
在标签类中,您应该覆盖AutoSize属性。
//(In C#)
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
public new bool AutoSize
{
get { return base.AutoSize; }
set { base.AutoSize = value; }
}
The browsable(false) will hide the property at design time and the DesignerSerializationVisibility attribute will tell the designer to not write any code into your designer file.
可浏览(false)将在设计时隐藏属性,DesignerSerializationVisibility属性将告诉设计者不要将任何代码写入设计器文件。
#2
I finally got this to work in VB. I had to disable the Set statement, essentially turning the Overridden AutoSize Property into a read-only property.
我终于让它在VB中工作了。我必须禁用Set语句,实际上将Overridden AutoSize属性转换为只读属性。
Public Overrides Property AutoSize() As Boolean
Get
Return MyBase.AutoSize
End Get
Set(ByVal value As Boolean)
'Do nothing here
End Set
End Property
Thanks to NascarEd for getting me pointed in the right direction.
感谢NascarEd让我指出了正确的方向。
#3
Just for your future info, to set the autosize property to False in the properties window, you need to set an attribute:-
仅为了将来的信息,要在属性窗口中将autosize属性设置为False,您需要设置一个属性: -
<System.ComponentModel.DefaultValue(False)> _
Public Overrides Property AutoSize() As Boolean ....
Public Overrides Property AutoSize()As Boolean ....
#4
If you go into the design mode for the new control you are creating, you should be able to select that control and change the properties however you'd like. From that point on, whenever you add that control to form (or another control) it will have the properties you set there as the default. This should allow you to set defaults while also keeping them visible so that developers can change things should they not want it to be resizeable in the future.
如果您进入正在创建的新控件的设计模式,您应该能够选择该控件并根据需要更改属性。从那时起,无论何时将该控件添加到表单(或其他控件),它都会将您在此处设置的属性设置为默认值。这应该允许您设置默认值,同时保持它们可见,这样开发人员可以在不希望将来可以调整大小的情况下进行更改。
Alternatively, check out the code generated by the designer, as it will show you exactly what it did to generate the behavior that you're looking for.
或者,查看设计器生成的代码,因为它将准确显示它为生成您正在寻找的行为所做的工作。