This is the code for a numeric only textbox
private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
But better create a custom control by adding a class and inherit textbox
and override its OnKeyPress Event
using System.Windows.Forms;
namespace Test
{
public partial class NumericTextBox : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
if (e.KeyChar == '.'
&& (this as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
}
}
you will get a new control on your toolbox