C#:
void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsControl(e.KeyChar))
return;
if (Char.IsDigit(e.KeyChar) && ((e.KeyChar & 0xFF) == e.KeyChar))
return;
if (e.KeyChar == '-')
{
if (((TextBox)sender).Text.Length <= 0)
return;
else
{
if (((TextBox)sender).Text.IndexOf('-') == -1 && ((TextBox)sender).SelectionStart == 0)
return;
}
}
e.Handled = true;
}
C++/CLR
void textBox_KeyPress(object^ sender, KeyPressEventArgs^ e)
{
if (Char::IsControl(e->KeyChar))