Customized TextBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I need to extend the behaviour of TextBox control and add to it the
option to accept only integer or float number and so on.

Does any one know if there is some code already written from which start or
better if there is some control with this functionality ready to use ?

Thank you in advance.

Keven Corazza
 
I have been looking for something similar myself but found nothing really
useful. Truth is it is quite hard to create a textbox with this
functionality because of some limitations in .NET Compact Framework.

If I understand Keven right, the article on code project won't help him very
much, especially if he is working in C#.

OpenNETCF.org has it's TextBoxEx that you can force to be "numeric only". It
does have some major limitations though, such as not beeing able to set
negative numbers and decimal numbers. Anyway, I think that is a better
start...

I'm actually working on this kind of TextBox, but I am quite a newbie when
it comes to creating custom controls, and it is not a final product yet for
a couple of days.

/ Peter
 
You could, with the help of a native DLL, subclass the EDIT control that
you're working with and have it respond to some extra messages telling it
whether float or integer data is to be accepted and to have it do the right
things to prevent alpha characters (for example), from being added to the
EDIT text.

Paul T.
 
The NumberOnlyTextBox class below subclasses the TextBox control so it only
accepts numbers:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;

public class Test : System.Windows.Forms.Form
{
public Test()
{
this.Text = "numbers only";
this.MinimizeBox = false;

NumberOnlyTextBox x = new NumberOnlyTextBox();
x.Parent = this;
}

static void Main()
{
Application.Run(new Test());
}
}


/// <summary>
/// Summary description for NumberOnlyTextBox.
/// </summary>
public class NumberOnlyTextBox : TextBox
{
bool allowSpace = false;

// Restricts the entry of characters to digits (including hex), the
negative sign,
// the decimal point, and editing keystrokes (backspace).
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);

NumberFormatInfo numberFormatInfo =
System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
string groupSeparator = numberFormatInfo.NumberGroupSeparator;
string negativeSign = numberFormatInfo.NegativeSign;

string keyInput = e.KeyChar.ToString();

if(Char.IsDigit(e.KeyChar))
{
// Digits are OK
}
else if(keyInput.Equals(decimalSeparator) ||
keyInput.Equals(groupSeparator) ||
keyInput.Equals(negativeSign))
{
// Decimal separator is OK
}
else if(e.KeyChar == '\b')
{
// Backspace key is OK
}
else if(this.allowSpace && e.KeyChar == ' ')
{

}
else
{
// Swallow this invalid key
e.Handled = true;
}
}

public int IntValue
{
get
{
return Int32.Parse(this.Text);
}
}

public decimal DecimalValue
{
get
{
return Decimal.Parse(this.Text);
}
}

public bool AllowSpace
{
set
{
this.allowSpace = value;
}

get
{
return this.allowSpace;
}
}
}
 
The NumberOnlyTextBox class below subclasses the TextBox control so it only
accepts numbers:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;

public class Test : System.Windows.Forms.Form
{
public Test()
{
this.Text = "numbers only";
this.MinimizeBox = false;

NumberOnlyTextBox x = new NumberOnlyTextBox();
x.Parent = this;
}

static void Main()
{
Application.Run(new Test());
}
}


/// <summary>
/// Summary description for NumberOnlyTextBox.
/// </summary>
public class NumberOnlyTextBox : TextBox
{
bool allowSpace = false;

// Restricts the entry of characters to digits (including hex), the
negative sign,
// the decimal point, and editing keystrokes (backspace).
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);

NumberFormatInfo numberFormatInfo =
System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
string groupSeparator = numberFormatInfo.NumberGroupSeparator;
string negativeSign = numberFormatInfo.NegativeSign;

string keyInput = e.KeyChar.ToString();

if(Char.IsDigit(e.KeyChar))
{
// Digits are OK
}
else if(keyInput.Equals(decimalSeparator) ||
keyInput.Equals(groupSeparator) ||
keyInput.Equals(negativeSign))
{
// Decimal separator is OK
}
else if(e.KeyChar == '\b')
{
// Backspace key is OK
}
else if(this.allowSpace && e.KeyChar == ' ')
{

}
else
{
// Swallow this invalid key
e.Handled = true;
}
}

public int IntValue
{
get
{
return Int32.Parse(this.Text);
}
}

public decimal DecimalValue
{
get
{
return Decimal.Parse(this.Text);
}
}

public bool AllowSpace
{
set
{
this.allowSpace = value;
}

get
{
return this.allowSpace;
}
}
}
 
I think there's still a hole there: copy/paste. And, of course, you can
have any number of decimal points in the text... Those things can be
addressed, though!

Paul T.
 
Back
Top