Click here to Skip to main content
Click here to Skip to main content

A numeric edit control

, 1 Jun 2005
Rate this:
Please Sign up or sign in to vote.
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

About the Author

kennedy_franklin
Web Developer
Kuwait Kuwait
I am J. Franklin Kennedy, Working as a programmer in application software developement.
 

Comments and Discussions

 
QuestionQuerry about LAN Messenger Pinmemberdiasroyal8-Jul-11 5:33 
GeneralDecimals Pinmembergregoryayca6-Jun-07 6:31 
GeneralRe: Decimals Pinmemberkennedy_franklin9-Jun-07 4:07 

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 2 Jun 2005
Article Copyright 2005 by kennedy_franklin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid