vb.netnumericupdowncustom-formatting

NumericUpDown with UpdateEditText()


I need to set two numericupdown controls to set two numbers with the following format:

  1. One with 000001 format
  2. The other with 001 format

I've found the UpdateTextEdit() control on MS Support page but I can't understand how to use it on vb.NET.


Solution

  • You have to derive your own Control from NumericUpDown and then override UpdateEditText(). This method is Protected. This means that it is only visible in derived classes (and in the original class of course).

    In the following example I also added a Format property which appears in the properties window under Appearance (after you have compiled the code for the first time).

    This new control also appears in the Toolbox and you can drag and drop it to your forms like a normal NumericUpDown.

    Imports System.ComponentModel
    Imports System.Windows.Forms
    
    Public Class FormattedNumericUpDown
        Inherits NumericUpDown
    
        Private _format As String
    
        <Category("Appearance")>
        Public Property Format As String
            Get
                Return _format
            End Get
            Set(value As String)
                _format = value
                UpdateEditText()
            End Set
        End Property
    
        Protected Overrides Sub UpdateEditText()
            Text = Value.ToString(_format)
        End Sub
    End Class
    

    enter image description here