Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A numeric edit control

0.00/5 (No votes)
1 Jun 2005 1  
This is a control which will allow the user to input only numbers. And it also has some special features like, if we enter negative values, we can show it in a different color.

Introduction

This is a control which will allow the user to input only numbers. And it also has some special features like, if we enter negative values, we can show it in a different color. We can also allow/restrict the Zero Value in this control, and also set the minimum and maximum values. If we press the �F2� key inside the control, it will bring the calculator, which we can make use of for doing some calculation.

Properties

  • MaximumValue

    Using this property we can set/get the maximum bound as an input to this control.

  • MinimumValue

    Using this property we can set/get the minimum bound as an input to this control.

  • NegativeColor

    This property is used to change the color of the control if the control is having a negative value.

    If the value in the control is positive, the control will look like below:

    If the value in the control is negative, the control will look like below:

    We can set any color as we like.

  • ThousandSeparator

    This is a boolean property where we can set if the thousand separator is necessary or not.

  • Value

    Set/get the value of the control as a number.

  • ZeroAllowed

    This is a boolean property. Using this we can set if the control can have a zero value or not. If we set the ZeroAllowed property to False, it won�t allow the user to input zero. And it also shows a warring message.

    If we place the mouse pointer in the exclamatory icon, it will show the warring message.

Given below is the screen shot of the property page for this control.

The Code

The main technical functionality of this control is as follows:

Private Overloads Sub NumTxt_TextChanged(ByVal sender _
        As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles NumTxt.KeyPress, MyBase.KeyPress

    Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
    Dim isDecimal As Boolean = e.KeyChar.ToString = "."
    Dim isBackSpace As Boolean = Asc(e.KeyChar) = "08"
    Dim isNegative As Boolean = e.KeyChar.ToString = "-"

    If isNegative And Len(NumTxt.Text) > 0 Then
        e.Handled = True
        Exit Sub
    End If

    If InStr(NumTxt.Text, ".") > 0 And isDecimal Then
        e.Handled = True
        Exit Sub
    End If

    If Not isKey And Not isBackSpace And Not isDecimal And Not isNegative Then
        e.Handled = True
    End If
    If NumTxt.Text <> "" Then
        lnOldVal = Val(Replace(NumTxt.Text, ",", ""))
    Else
        lnOldVal = 0
    End If
End Sub

How to use this control in your projects

Just add the reference of this control, using the Project �> Add Reference option (.NET).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here