|
//
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER
// REMAINS UNCHANGED.
//
// Email: yetiicb@hotmail.com
//
// Copyright (C) 2002-2003 Idael Cardoso.
//
using System;
using System.Windows.Forms;
using System.Globalization;
namespace Yeti.Controls
{
/// <summary>
/// Define a TextBox that allow only integer numbers.
/// </summary>
public class NumericTextBox : TextBox
{
public NumericTextBox()
:base()
{
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if ( (!e.Handled) && ( "1234567890\b".IndexOf(e.KeyChar) < 0) )
{
Yeti.Sys.Win32.MessageBeep(Yeti.Sys.BeepType.SimpleBeep);
e.Handled = true;
}
}
public event EventHandler FormatError;
public event EventHandler FormatValid;
protected virtual void OnFormatError(EventArgs e)
{
if (FormatError != null)
{
FormatError(this, e);
}
}
protected virtual void OnFormatValid(EventArgs e)
{
if (FormatValid != null)
{
FormatValid(this, e);
}
}
protected override void OnTextChanged(EventArgs e)
{
try
{
int.Parse(this.Text, NumberStyles.Integer);
OnFormatValid(e);
}
catch
{
OnFormatError(e);
}
base.OnTextChanged (e);
}
public int Value
{
get
{
return int.Parse(this.Text, NumberStyles.Integer);
}
set
{
this.Text = value.ToString();
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.