我试图以几种方式子类化NumericUpDown以获得更好的功能和外观. 由于NUD是两个控件的构造,我想在属性“Increment”设置为0的情况下隐藏上/下按钮. 此代码位于子类中: Protected Overrides Sub
由于NUD是两个控件的构造,我想在属性“Increment”设置为0的情况下隐藏上/下按钮.
此代码位于子类中:
Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs) Controls(0).Hide() End Sub
……它运作正常.
但在该函数中,我无法像这样检查Increment属性的值:
Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs) If Me.Increment = 0 Then Controls(0).Hide() End if End Sub
在此功能的范围内,我无法访问.
我也尝试使用局部变量,但无法在OnTextBoxResize之前找到触发哪个事件来读取Increment属性的值.
在这种情况下该怎么做才能获得所需的功能?
这似乎运作得相当好.它将增量属性设置为阴影,以便在更改增量值时设置微调控件的可见性.基本控件调用一个名为PositionControls的底层私有方法,你无法阻止 – 该方法可能会产生一些闪烁,但在我的测试中,它没有.Public Class MyNumBox Inherits NumericUpDown Shadows Property Increment As Decimal Get Return MyBase.Increment End Get Set(value As Decimal) MyBase.Increment = value OnTextBoxResize(Me, EventArgs.Empty) End Set End Property Protected Overrides Sub OnHandleCreated(e As EventArgs) MyBase.OnHandleCreated(e) OnTextBoxResize(Me, EventArgs.Empty) End Sub Protected Overrides Sub OnTextBoxResize(source As Object, e As EventArgs) If Me.IsHandleCreated Then Me.Height = Me.PreferredHeight Me.Controls(0).Visible = (MyBase.Increment > 0) Dim borderWidth As Integer = 0 If Me.BorderStyle > BorderStyle.None Then borderWidth = SystemInformation.Border3DSize.Width End If Dim textWidth As Integer If Me.Increment = 0 Then textWidth = Me.ClientSize.Width - (borderWidth * 2) Else textWidth = Me.ClientSize.Width - Me.Controls(0).Width - (borderWidth * 2) End If If Me.UpDownAlign = LeftRightAlignment.Left Then If Me.Increment = 0 Then Me.Controls(1).SetBounds(borderWidth, borderWidth, _ textWidth, Me.Controls(1).Height) Else Me.Controls(1).SetBounds(borderWidth + Me.Controls(0).Width, _ Me.Controls(1).Top, textWidth, Me.Controls(1).Height) End If Else Me.Controls(1).SetBounds(borderWidth, Me.Controls(1).Top, _ textWidth, Me.Controls(1).Height) End If Me.Refresh() End If End Sub End Class
在OnTextBoxResize重写中,我将控件重新定位到适当的位置,此版本确实考虑了UpDownAlign属性.