Click here to Skip to main content
Licence CPOL
First Posted 13 Dec 2006
Views 17,938
Downloads 77
Bookmarked 20 times

PercentageUpDown Control

By | 29 Jun 2009 | Article
Specialized version of the NumericUpDown control for entering and displaying percentage values.

Sample Image - PercentageUpDown.jpg

Introduction

I have been looking for a good way for entering percentages in the same way as numeric values are entered. In the past, I used a normal NumericUpDown, put a label behind it with "%", and did the mathematics of multiplying with and dividing by 100 in the form.

There is, of course, a better way to do it. Maybe, it's because it is so simple that nobody has published the source code for it on CodeProject. The control I present here also makes it easy to bind to floating point fields in a dataset.

By the way, I made the control look properly themed with Skybound's VisualStyles. To make it work, you have to put Skybound.VisualStyles.VisualStyleContext.Create() between the form's MyBase.New and InitializeComponent.

Source Code

Because it is so small, I give you the complete source code listing here. It is written in VB.NET, but I'm sure you can convert it to C#.

Public Class PercentageUpDown _
    Inherits Windows.Forms.NumericUpDown

    Public Overrides Sub DownButton()
        If Not Me.ReadOnly Then
            MyBase.DownButton()
        End If
    End Sub

    Public Overrides Sub UpButton()
        If Not Me.ReadOnly Then
            MyBase.UpButton()
        End If
    End Sub

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text.TrimEnd("%")
        End Get
        Set(ByVal Value As String)
            MyBase.Text = Value & "%"
        End Set
    End Property

    <System.ComponentModel.Category("Data"), _
     System.ComponentModel.DefaultValue(0.0)> _
    Public Shadows Property Value() As Double
        Get
            Return MyBase.Value / 100
        End Get
        Set(ByVal Value As Double)
            MyBase.Value = Value * 100
        End Set
    End Property
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pinx

Web Developer

Netherlands Netherlands

Member

I have been a VB.NET programmer for quite some years. I work with a small firm, that specializes in supply chain planning for bulk products. We have our own application, VMIScheduler, that helps vehicle planners with their daily job.
I am married and I have two children.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to use Pinmember8844318:03 16 Aug '07  
QuestionAny idea how to make a AlphabeticUpDown Class? PinmemberBob Schader6:52 13 Apr '07  
I am looking into creating a custom up/down class that would handle the use of alphabetic characters INSTEAD of numbers. Do you know if I can modify the NumericUpDown control like you have done, or do I need to start with it's Base Class (UpDownBase)?
 
I have not done this type of programming recently and am fairly new to VB.Net. Thanks in advance for your answers.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 30 Jun 2009
Article Copyright 2006 by Pinx
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid