Code:
public class TextBoxEx : TextBox
{
public enum TextBoxExFilterType : byte
{
NoFilter,
WholeNumber,
DecimalNumber,
PositiveWholeNumber,
PositiveDecimalNumber,
NegativeWholeNumber,
NegativeDecimalNumber,
CustomFilter
}
bool _changeInPorgress = false;
byte _decimalPlaces = 255;
string _currentFilterPattern = string.Empty;
string _customFilterPattern = string.Empty;
TextBoxExFilterType _filterType = TextBoxExFilterType.NoFilter;
public TextBoxEx()
{
}
protected override void OnEnter(EventArgs e)
{
this.SelectionStart = 0;
this.SelectionLength = this.Text.Length;
base.OnEnter(e);
}
protected override void OnTextChanged(EventArgs e)
{
if (_filterType == TextBoxExFilterType.NoFilter)
goto End;
if (_changeInPorgress)
{
_changeInPorgress = false;
goto End;
}
Regex regex = new Regex(_currentFilterPattern);
string match = regex.Match(this.Text).Value;
if (match != this.Text)
{
_changeInPorgress = true;
this.Text = match;
}
End:
base.OnTextChanged(e);
}
// ovo je da se ne bi cuo onaj glubi beep kada se pritisne enter
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Enter)
e.SuppressKeyPress = true;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == '\b' || _filterType == TextBoxExFilterType.NoFilter)
goto End;
StringBuilder sb = new StringBuilder();
sb.Append(this.Text.Substring(0, this.SelectionStart));
sb.Append(e.KeyChar);
sb.Append(this.Text.Substring(this.SelectionStart + this.SelectionLength));
string text = sb.ToString();
Regex regex = new Regex(_currentFilterPattern);
if (regex.Match(text).Value != text)
{
e.Handled = true;
System.Media.SystemSounds.Beep.Play();
}
else
_changeInPorgress = true;
End:
base.OnKeyPress(e);
}
[Category("Filter")]
[DefaultValue(TextBoxEx.TextBoxExFilterType.NoFilter)]
public TextBoxExFilterType FilterType
{
get
{
return _filterType;
}
set
{
_filterType = value;
_currentFilterPattern = GetFilterPattern(_filterType, _decimalPlaces);
//refresh
Regex regex = new Regex(_currentFilterPattern);
this.Text = regex.Match(this.Text).Value;
}
}
[Category("Filter")]
[DefaultValue("")]
public string CustomFilter
{
get
{
return _customFilterPattern;
}
set
{
_customFilterPattern = value;
if (_filterType == TextBoxExFilterType.CustomFilter)
{
_currentFilterPattern = _customFilterPattern;
//refresh
Regex regex = new Regex(_currentFilterPattern);
this.Text = regex.Match(this.Text).Value;
}
}
}
[Category("Filter")]
[DefaultValue((byte)255)]
public byte DecimaPlaces
{
get
{
return _decimalPlaces;
}
set
{
if (value < 1)
{
throw new Exception("Decimal places can not be smaller then 1.");
}
_decimalPlaces = value;
_currentFilterPattern = GetFilterPattern(_filterType, _decimalPlaces);
//refresh
Regex regex = new Regex(_currentFilterPattern);
this.Text = regex.Match(this.Text).Value;
}
}
string GetFilterPattern(TextBoxExFilterType filter, byte decimalPlaces)
{
NumberFormatInfo nfi = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat;
switch (filter)
{
case TextBoxExFilterType.NoFilter:
return "";
case TextBoxExFilterType.WholeNumber:
return @"[" + nfi.NegativeSign + @"]?\d*";
case TextBoxExFilterType.DecimalNumber:
return @"[" + nfi.NegativeSign + @"]?\d*[" + nfi.NumberDecimalSeparator + @"]?\d{0," + decimalPlaces.ToString() + "}";
case TextBoxExFilterType.NegativeWholeNumber:
return @"[" + nfi.NegativeSign + @"]\d*";
case TextBoxExFilterType.NegativeDecimalNumber:
return @"[" + nfi.NegativeSign + @"]\d*[" + nfi.NumberDecimalSeparator + @"]?\d{0," + decimalPlaces.ToString() + "}";
case TextBoxExFilterType.PositiveWholeNumber:
return @"\d*";
case TextBoxExFilterType.PositiveDecimalNumber:
return @"\d*[" + nfi.NumberDecimalSeparator + @"]?\d{0," + decimalPlaces.ToString() + "}";
case TextBoxExFilterType.CustomFilter:
return _customFilterPattern;
}
return "";
}
}
Evo ti cela kontrola.
Only Time Will Tell