Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / C#
Article

Numeric TextBox

Rate me:
Please Sign up or sign in to vote.
4.46/5 (12 votes)
22 Sep 2008CPOL1 min read 43.9K   1.5K   28   14
The textbox that accepts numbers and uses separated character for reading easily
Image 1

Introduction

I needed one textbox that accepts a number and uses separated character on each three numbers as right.

I searched for this on The Code Project, but couldn't find any such article. So I decided to create this one.

Notice: Numeric TextBox is not MasktextBox, this textBox is dynamic but masktextbox is static. When you changing a number, the position of the separated character changes.

Using the Code

property.jpg

Add numericTextBox in your project, then drag and drop on the form.

Numeric Textbox contains these properties:

  1. MaxValue: With this property, you could enter max of a number as per your requirement.
  2. Invalidsound: In this property, you could set invalid sound for when the user presses the wrong key, e.g. when pressing a letter character.
  3. SeparatedChar: A separated character is a character that is placed between numbers. Default of separated character is ",".
  4. Value: The value of a numeric textbox that you see has Int64 type. When you want to use Int32 type, use this:
C#
(Int32)numericTextBox.Value 

How It Works

NumericTextBox is a textbox that manages the keyPressed event:

C#
protected override void OnKeyPress(KeyPressEventArgs e)
{
            isKeyPress = true;
            base.OnKeyPress(e);
            double val = (this.Text.Length == 0 ? 0 : 
		        double.Parse(this.Text.Replace(sepratedChar.ToString(), "")));

            if (Char.IsDigit(e.KeyChar) && val * 10 + 
			int.Parse(e.KeyChar.ToString()) <= maxValue)
            {
                // Digits are OK
            }
            else if (e.KeyChar == '\b')
            {
                // Backspace key is OK

            }
            else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
            {
                //ModifierKeys + ( Alt & Ctrl ) are OK
            }
            else if (this.SelectionLength > 0)
            {
                // Selected Text OK
            }

            else
            {
                e.handle = true;
            }

Then when text is changed, call SetSepratedChar procedure, in SetSepratedChar while  Value>1000, divide by 1000 and insert SeparatedChar but it's different between digits and Del Key and backspace button, because selectionstart is a different position.

C#
private void SetSepratedChar(int index)
{
	isKeyPress = false;
	isDelKeyPress = false;
	Int64 intValue = Value;
	int selectionStart = this.SelectionStart;

	this.Clear();
	while (intValue >= 1000)
	{
		Int64 mod = intValue % 1000;
		if (mod == 0)
		this.Text = this.Text.Insert(0, sepratedChar.ToString() + "000");
		else if (mod.ToString().Length == 1)this.Text = 
		this.Text.Insert(0, sepratedChar.ToString() + "00" + mod.ToString());

		else if (mod.ToString().Length == 2)
			this.Text = this.Text.Insert(0, 
			sepratedChar.ToString() + "0" + mod.ToString());
		else
			this.Text = this.Text.Insert(0, 
			sepratedChar.ToString() + mod.ToString());

		intValue = intValue / 1000;
	}

	this.Text = this.Text.Insert(0, intValue.ToString());
	this.SelectionStart = selectionStart + index;
}

I needed an integer number, but if you think this component is useful for decimal numbers, please let me know.

Please give me some tips.

poem.jpg

License

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


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow about MinValue? Pin
Romi Ardiansya9-Aug-17 12:30
Romi Ardiansya9-Aug-17 12:30 
Questionhi Pin
Member 101936566-Aug-13 19:05
Member 101936566-Aug-13 19:05 
AnswerRe: hi Pin
Ehsan Golkar6-Aug-13 19:58
Ehsan Golkar6-Aug-13 19:58 
GeneralRe: hi Pin
Member 101936566-Aug-13 21:23
Member 101936566-Aug-13 21:23 
GeneralRe: hi Pin
Ehsan Golkar6-Aug-13 21:28
Ehsan Golkar6-Aug-13 21:28 
i am not coding C# anymore, and I am not living in Iran now.
You can find it by looking at other people code easily, it is not difficult, but if you cope with any problem, contact me.
Regards
GeneralMy vote of 5 Pin
FM710-Aug-12 21:56
FM710-Aug-12 21:56 
GeneralRe: My vote of 5 Pin
Ehsan Golkar20-Jan-13 19:59
Ehsan Golkar20-Jan-13 19:59 
Questionعالی بود Pin
haidreza15-Jun-12 21:34
haidreza15-Jun-12 21:34 
AnswerRe: عالی بود Pin
Ehsan Golkar10-Jan-13 3:05
Ehsan Golkar10-Jan-13 3:05 
Generalvery good Pin
lymadalin29-Sep-08 20:47
lymadalin29-Sep-08 20:47 
GeneralRe: very good Pin
lymadalin5-Oct-08 16:09
lymadalin5-Oct-08 16:09 
GeneralInternationalization Pin
TobiasP29-Sep-08 7:14
TobiasP29-Sep-08 7:14 
GeneralRe: Internationalization Pin
Ehsan Golkar29-Dec-08 6:54
Ehsan Golkar29-Dec-08 6:54 

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.