using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
namespace ClientCtrlComponents
{
public partial class EditMoney : TextBox
{
#region Member Variables
Color originalbgColor = Color.White;
Color lostfocusbgColor = Color.Ivory;
Color watermarkColor = Color.Gray;
Color forecolor;
Font font;
#endregion
#region Constructor
public EditMoney()
{
this.originalbgColor = this.FocusOnColor;
this.lostfocusbgColor = this.FocusLostColor;
this.forecolor = this.ForeColor;
this.ForeColor = this.watermarkColor;
this.font = this.Font;
this.KeyPress += new KeyPressEventHandler(EditMoney_KeyPress);
this.GotFocus += new EventHandler(EditMoney_GotFocus);
this.Leave += new EventHandler(EditMoney_Leave);
}
#endregion
#region Event Handler Methods
private void EditMoney_GotFocus(object sender, EventArgs e)
{
FocusColor(true);
}
private void EditMoney_Leave(object sender, EventArgs e)
{
decimal d;
FocusColor(false);
if (decimal.TryParse(((TextBox)sender).Text, out d))
((TextBox)sender).Text = d.ToString();
}
private void FocusColor(bool focus)
{
this.BackColor = (focus && this.Focus() ? this.originalbgColor : this.lostfocusbgColor);
}
void EditMoney_KeyPress(object sender, KeyPressEventArgs e)
{
NumberFormatInfo numberFormatInfo =
System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
string groupSeparator = numberFormatInfo.NumberGroupSeparator;
string negativeSign = numberFormatInfo.NegativeSign;
e.Handled =
!(e.KeyChar.Equals((char)8)) &&
!Char.IsDigit(e.KeyChar) &&
!(e.KeyChar == (char)46 && (((TextBox)sender).Text.IndexOf(decimalSeparator) < 0));
try
{
if (((TextBox)sender).Text.Length == 0 && e.KeyChar.Equals((char)48)) //0
{
e.Handled = true;
}
else
if (((TextBox)sender).Text.Length == 0 && e.KeyChar.ToString().Equals(decimalSeparator)) //.
{
((TextBox)sender).Text = ((char)48).ToString() + decimalSeparator;
((TextBox)sender).Select(((TextBox)sender).Text.Length, 0);
e.Handled = true;
}
else
if (!(e.KeyChar.Equals((char)8)) &&
(((TextBox)sender).Text.Substring(((TextBox)sender).Text.IndexOf(decimalSeparator)).Length >= 3))
{
e.Handled = true;
}
}
catch { }
}
#endregion
#region User Defined Properties
/// <summary>
/// Property to set/get background color at focus on at design/runtime
/// </summary>
[Browsable(true)]
[Category("Extended Properties")]
[Description("Sets background color on focus")]
[DisplayName("FocusOnColor")]
public Color FocusOnColor
{
get
{
return this.originalbgColor;
}
set
{
this.originalbgColor = value;
base.OnGotFocus(new EventArgs());
}
}
/// <summary>
/// Property to set/get background color at focus lost at design/runtime
/// </summary>
[Browsable(true)]
[Category("Extended Properties")]
[Description("Sets background lost focus color")]
[DisplayName("FocusLostColor")]
public Color FocusLostColor
{
get
{
return this.lostfocusbgColor;
}
set
{
this.lostfocusbgColor = value;
base.OnLeave(new EventArgs());
}
}
#endregion
}
}