martedì 30 settembre 2008

CSharp NumericTextBox control

I've post here a simple custom control that it accepts only numeric input.
The only one methods in the OnKeyPress (overrided) and accept only digit or the backspace
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using DrLuiji.Controls
{
public class NumericTextBox : TextBox
{
protected
override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (!((Char.IsDigit(e.KeyChar))||(e.KeyChar.Equals('\b'))))
{
e.Handled = true;
ErrorBeep();
}
}
public void ErrorBeep()
{
//Console.Beep();
}
}
}

To use it you just drag & drop the control from the toolbox to your form.

Have fun.

Nessun commento: