Numeric text box means, a Text Box which can only accepts numeric values.
Code to implement a numeric text box.
Code should be written on textbox keypress event.
privatevoid textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) == false && e.KeyChar != '.' && e.KeyChar!='\b')
//checking whether pressed key is number, decimal or backspace.
//If false then we will set Handled variable of KeyPressEventArgs to true.
//By doing this system will get a message that character of keypressed
//is executed and hence aplpa numeric character will not be displayed
//except decimal.
e.Handled=true;
elseif (e.KeyChar == '.')
if (textBox1.Text.Contains('.'))
e.Handled=true;
//checking whether decimal key is pressed or not, if true then checking
//whether decimal is already present or not, as we can have only
//one decimal point in a text box.
}
Anonymous User
29-Jun-2012Replace your code with this code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) == false && e.KeyChar != '.' && e.KeyChar != '\b' && e.KeyChar != '+')
//checking whether pressed key is number, decimal backspace or '+'.
//If false then we will set Handled variable of KeyPressEventArgs to true.
//By doing this system will get a message that character of keypressed
//is executed and hence aplpa numeric character will not be displayed
//except decimal. '+' is added by Rao Inala MD. .
e.Handled = true;
else if (e.KeyChar == '.')
{
if (textBox1.Text.Contains('.'))
e.Handled = true;
}
else if (e.KeyChar == '+')
if (textBox1.Text.Contains('+'))
e.Handled = true;
//checking whether decimal key is pressed or not, if true then checking
//whether decimal is already present or not, as we can have only
//one decimal point in a text box.
// I add this code to check whether '+' key is pressed or not , if true then checking
// whether '+' is already present or not . we can have only one +.
//My current code is entering more than one' '+ . How to solve this problem?
}
Now it will work as you want.
rao Inala
29-Jun-2012namespace Samples_Mindstick_NumericTextbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) == false && e.KeyChar != '.' && e.KeyChar != '\b' && e.KeyChar != '+')
//checking whether pressed key is number, decimal backspace or '+'.
//If false then we will set Handled variable of KeyPressEventArgs to true.
//By doing this system will get a message that character of keypressed
//is executed and hence aplpa numeric character will not be displayed
//except decimal. '+' is added by Rao Inala MD. .
e.Handled = true;
else if (e.KeyChar == '.')
if (textBox1.Text.Contains('.'))
e.Handled = true;
else if (e.KeyChar == '+')
if (textBox1.Text.Contains('+'))
e.Handled = true;
//checking whether decimal key is pressed or not, if true then checking
//whether decimal is already present or not, as we can have only
//one decimal point in a text box.
// I add this code to check whether '+' key is pressed or not , if true then checking
// whether '+' is already present or not . we can have only one +.
//My current code is entering more than one' '+ . How to solve this problem?
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}