Hiding up / down buttons in a NumericUpDown element

I am trying to subclass NumericUpDown in several ways to improve functionality and appearance.

Since NUD is the construction of two controls, I would like to hide the up / down buttons in cases where the "Increment" property is set to 0.

This code is in a subclass:

Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)

        Controls(0).Hide()
End Sub

... and it works fine. But in this function, I cannot check the value of the Increment property as follows:

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

Me is not available under this feature. I am also trying to use local variables, but I cannot find which event will be fired before OnTextBoxResize to read the value of the Increment property.

What to do in this case to get the desired functionality?

+4
2

, , . Shadows the Increment, 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.

+4

, EE, . , , , . * , , .

VisibleChanged() Increment(). :

Public Class NoArrowNumericUpDown
    Inherits NumericUpDown

    Private itb As InnerTextBox = Nothing

    Protected Overrides Sub OnVisibleChanged(e As System.EventArgs)
        If Me.Visible Then
            If Me.Increment = 0 AndAlso IsNothing(itb) Then
                Dim ctl As Control = Me.Controls(0) ' get the spinners
                Me.Controls.Remove(ctl)  ' remove the spinners
                ctl = Me.Controls(0) ' get the edit control
                itb = New InnerTextBox(Me, ctl)
            End If
        End If

        MyBase.OnVisibleChanged(e)
    End Sub

    Public Class InnerTextBox
        Inherits NativeWindow

        Private parentControl As Control = Nothing
        Const WM_WINDOWPOSCHANGING As Integer = &H46

        Public Sub New(parentControl As Control, InnerTextBox As Control)
            Me.parentControl = parentControl
            Me.AssignHandle(InnerTextBox.Handle)
        End Sub

        Protected Overrides Sub WndProc(ByRef m As Message)
            Select Case m.Msg
                Case WM_WINDOWPOSCHANGING
                    Dim wp As WindowPos = CType(System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, GetType(WindowPos)), WindowPos)
                    If Me.parentControl IsNot Nothing Then
                        wp.cx = Me.parentControl.ClientSize.Width - 2 * wp.x
                        wp.cy = Me.parentControl.ClientSize.Height
                        System.Runtime.InteropServices.Marshal.StructureToPtr(wp, m.LParam, True)
                    End If
                    Exit Select
            End Select

            MyBase.WndProc(m)
        End Sub

        Public Structure WindowPos
            Public hwnd As IntPtr
            Public hwndInsertAfter As IntPtr
            Public x As Integer
            Public y As Integer
            Public cx As Integer
            Public cy As Integer
            Public flags As UInteger
        End Structure

    End Class

End Class

EDIT: Try/Catch?

Public Class NoArrowNumericUpDown
    Inherits NumericUpDown

    Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)
        Try
            If Me.Increment = 0 Then
                Controls(0).Hide()
            End If
        Catch ex As Exception
        End Try
    End Sub

End Class
+1

All Articles