-- VS2005
OK....VS2005 has a new maskedtextbox control - but it's not behaving as I thought it wound if I set it for decimal inpput. Maybe I'm overloooking something.
Here's what I did to subclass the textbox control. My class has a public property for the datatable and datacolumn source, and a boolean for whether the column takes decimals. If it does, I use two handlers to convert back and forth between currency and string. So anytime I needed to do any databinding to a decimal datatable column, the two handlers would take care of it.
Let me know if this helps...
Kevin
public class cgsTextBox : System.Windows.Forms.TextBox
{
public override Font Font
{
get {return base.Font; }
set {base.Font = value; }
}
private DataTable m_DtSource;
public DataTable DtSource
{
get {return m_DtSource ;}
set {m_DtSource = value; }
}
private DataColumn m_DcSource;
public DataColumn DcSource
{
get {return m_DcSource ;}
set {m_DcSource = value; }
}
private bool m_lDecimal;
public bool lDecimal
{
get {return m_lDecimal ;}
set {m_lDecimal = value; }
}
public cgdTextBox()
{
base.Font = new Font("Verdana",8);
this.Enter += new EventHandler(OnEnter);
this.Leave += new EventHandler(OnLeave);
this.KeyDown += new KeyEventHandler(OnKeyDown);
}
public void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
if(cevent.DesiredType != typeof(string)) return;
cevent.Value = ((decimal) cevent.Value).ToString("F2");
}
public void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
{
if(cevent.DesiredType != typeof(decimal)) return;
cevent.Value = Decimal.Parse(cevent.Value.ToString(),NumberStyles.Currency, null);
}
public void OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
SendKeys.Send("{TAB}");
}
public void OnEnter(object sender,EventArgs e )
{
this.SelectAll();
this.BackColor = Color.Yellow;
}
public void OnLeave(object sender,EventArgs e )
{
this.BackColor = Color.White;
}
public void BindControl()
{
if(this.m_DcSource != null)
{
this.DataBindings.Clear();
System.Windows.Forms.Binding newBinding ;
newBinding =
new System.Windows.Forms.Binding( "Text",this.m_DtSource,this.m_DcSource.ColumnName.ToString());
if(this.lDecimal==true)
{
this.TextAlign = HorizontalAlignment.Right;
newBinding.Format += new ConvertEventHandler(this.DecimalToCurrencyString);
newBinding.Parse += new ConvertEventHandler(this.CurrencyStringToDecimal);
}
this.DataBindings.Add(newBinding);
}
}
}