Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / Visual Basic
Article

A numeric edit control

Rate me:
Please Sign up or sign in to vote.
2.54/5 (7 votes)
1 Jun 20052 min read 36.2K   202   13   3
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:

VB
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


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


Comments and Discussions

 
QuestionQuerry about LAN Messenger Pin
diasroyal8-Jul-11 5:33
diasroyal8-Jul-11 5:33 
GeneralDecimals Pin
gregoryayca6-Jun-07 6:31
gregoryayca6-Jun-07 6:31 
GeneralRe: Decimals Pin
kennedy_franklin9-Jun-07 4:07
kennedy_franklin9-Jun-07 4:07 

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

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