![]() |
Desktop Development »
Edit Controls »
General
Beginner
License: The Code Project Open License (CPOL)
PercentageUpDown ControlBy PinxSpecialized version of the NumericUpDown control for entering and displaying percentage values. |
VB, Windows, .NET, Visual-Studio, WinForms, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
![]()
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.
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
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 29 Jun 2009 Editor: Smitha Vijayan |
Copyright 2006 by Pinx Everything else Copyright © CodeProject, 1999-2010 Web21 | Advertise on the Code Project |