2010-08-15, 03:37 PM
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Project
{
public class NumberBox : TextBox
{
public NumberBox()
{
this.KeyPress += new KeyPressEventHandler(NumberBox_KeyPress);
}
private void NumberBox_KeyPress(object sender, KeyPressEventArgs kpe)
{
int KeyCode = (int)kpe.KeyChar;
if (!IsNumberInRange(KeyCode, 48, 57) && KeyCode != 8)
{
kpe.Handled = true;
}
}
private bool IsNumberInRange(int Val, int Min, int Max)
{
return (Val >= Min && Val <= Max);
}
}
}This is a special textbox class that only allows numerical input. It uses the KeyPress event and compares the keycode to valid inputs; it therefore allows the use of backspace and all numerics, but I do not believe it allows for decimals. I believe you can simply find what keycode the period is on and set that to allow, and have another check somewhere to disable the use of more than one decimal for this.

