NumericUpDown control

  • Thread starter Thread starter bnob
  • Start date Start date
B

bnob

It is possible to format the numeric value of the NumericUpDown control

For example : display numbers in 2 digits, instead of 1 display 01, 0
-> 00 , 9 -> 09

Any idea
 
Looks like the control only offers Hexadecimal, DecimalPlaces, and
ThousandsSeparator properties to control the format, not custom
formats.

You can probably subclass the control, add a Format property, and
override the UpdateEditText method to support a custom format like you
want.

However, since the ParseEditText property is not declared as
overridable, you can not add the logic needed to parse a totally
custom format--the format must be compatible with the default
Decimal.Parse() method.

If you do need an incompatible format, then I think the only option is
to create an entirely new NumericUpDown control that inherits
UpDownBase. It's a shame that so many of the windows controls methods
are not declared overridable--makes extending them difficult.

HTH,

Sam
 
Easy. Just Overrides Text property

Public Class myNumericUpDown
Inherits System.Windows.Forms.NumericUpDown

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
End Sub

#End Region

Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal Value As String)
If Value.Length = 1 Then
MyBase.Text = "0" + Value
Else
MyBase.Text = Value
End If
End Set
End Property
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top