.NET 继承的标签不会自动调整大小
.NET inherited label doesn't autosize
我想从 Windows.Forms.Label 继承,所以我做了类似的东西:
Public Class CustomLabel
Inherits Label
Public Property CustomText As String
Protected Property DefaultText as
Public Overrides Property Text As String
Get
Return If(CustomText <> "", CustomText, MyBase.Text)
End Get
Set(value As String)
MyBase.Text = value
End Set
End Property
End Class
问题在于使用它,即使 AutoSize 属性 仍然为真,无论为 CustomText 设置什么值,标签都会保持其原始大小。
所以我认为当 CustomText 设置为 Mybase.Text 属性 时不会更新 我已更改为 :
Public Class CustomLabel
Inherits Label
Public Property CustomText As String
Get
Return _txt
End Get
Set(value As String)
_txt = value
MyBase.Text = value
End Set
End Property
Protected _txt As String
Protected Property DefaultText As String
Public Overrides Property Text As String
Get
Return If(CustomText <> "", CustomText, DefaultText)
End Get
Set(value As String)
DefaultText = value
End Set
End Property
End Class
但还是一样的问题
另一方面,当我在代码中设置 CustomLabel 的同一位置的标准标签上设置文本 属性 时,自动调整大小有效。
有什么想法吗?谢谢
经过一些其他尝试,我检查了 TextChanged 事件是如何引发的。看起来控件首先由设计者初始化,CustomText 设置为空,并且正如预期的那样引发了 TextChanged 事件。
然而,稍后在代码中当 CustomText 属性 设置为想要的值时,我不知道为什么,但事件没有引发,甚至如果字符串完全不同。
所以为了得到一个工作代码我必须写:
Public Property CustomText As String
Get
Return _txt
End Get
Set(value As String)
_txt = value
OnTextChanged(EventArgs.Empty) 'instead of Mybase.Text = value
End Set
End Property
强制事件然后触发标签调整大小。
所以它看起来已经解决了,但如果有人有解释,我很乐意知道。
我想从 Windows.Forms.Label 继承,所以我做了类似的东西:
Public Class CustomLabel
Inherits Label
Public Property CustomText As String
Protected Property DefaultText as
Public Overrides Property Text As String
Get
Return If(CustomText <> "", CustomText, MyBase.Text)
End Get
Set(value As String)
MyBase.Text = value
End Set
End Property
End Class
问题在于使用它,即使 AutoSize 属性 仍然为真,无论为 CustomText 设置什么值,标签都会保持其原始大小。
所以我认为当 CustomText 设置为 Mybase.Text 属性 时不会更新 我已更改为 :
Public Class CustomLabel
Inherits Label
Public Property CustomText As String
Get
Return _txt
End Get
Set(value As String)
_txt = value
MyBase.Text = value
End Set
End Property
Protected _txt As String
Protected Property DefaultText As String
Public Overrides Property Text As String
Get
Return If(CustomText <> "", CustomText, DefaultText)
End Get
Set(value As String)
DefaultText = value
End Set
End Property
End Class
但还是一样的问题
另一方面,当我在代码中设置 CustomLabel 的同一位置的标准标签上设置文本 属性 时,自动调整大小有效。
有什么想法吗?谢谢
经过一些其他尝试,我检查了 TextChanged 事件是如何引发的。看起来控件首先由设计者初始化,CustomText 设置为空,并且正如预期的那样引发了 TextChanged 事件。
然而,稍后在代码中当 CustomText 属性 设置为想要的值时,我不知道为什么,但事件没有引发,甚至如果字符串完全不同。
所以为了得到一个工作代码我必须写:
Public Property CustomText As String
Get
Return _txt
End Get
Set(value As String)
_txt = value
OnTextChanged(EventArgs.Empty) 'instead of Mybase.Text = value
End Set
End Property
强制事件然后触发标签调整大小。
所以它看起来已经解决了,但如果有人有解释,我很乐意知道。