Question numeric up and down

inkedgfx

Well-known member
Joined
Sep 29, 2012
Messages
139
Location
USA
Programming Experience
Beginner
I need some help with adding "%" to a numeric up and down control...is it possible to add this to the value of the control in code.

example:

if the value of the numeric up and down control is "25" then I need to add "%" the percent symbol to the end like this 25%

is it possible?

any help is appreciated

InkedGFX
 
If you want to display anything other than just plain numbers then you'd have to use a DomainUpDown instead and populate it yourself with the values you want displayed. I would tend to just put a Label to the right of the NUD to contain the % symbol.
 
Note that you could also create a user control, add a NumericUpDown and a textbox (or even RichTextBox!) anchored on top of the normal display area of a NUD. Then you just need to connect the two by copying the NUD value to the formatted textbox everytime the NUD is updated, and use that as a replacement.

EDIT: After some playing around it's even easier than that.

Public Class FormattedNumericUpDown
    Inherits NumericUpDown

    Public Property TextFormat As String

    Protected Overrides Sub UpdateEditText()
        If Not String.IsNullOrEmpty(Me.TextFormat) Then
            Me.Text = Me.Value.ToString(Me.TextFormat)
        Else
            MyBase.UpdateEditText()
        End If
    End Sub
End Class


Now just build the project, and the FormattedNumericUpDown control should appear in the designer toolbox. Stick a standard or custom string format into the new .TextFormat property, like ' 0 "%" ' ... This is a neat little trick, as the .Text property is hidden by intellisense, but still accessible on a NUD.
 
Last edited:
Back
Top