In .NET you need to write custom ConvertEventHandler's to
handle the formating and parsing. The two events that you use are the Binding.Format and Binding.Parse events. To display your textbox for currency you would do the following:
// Creates the binding first. MyColumnName is a Decimal type.
Binding b = new Binding
("Text", ds, "MyTableName.MyColumnName");
// Add the delegates to the event.
b.Format += new ConvertEventHandler(DecimalToCurrencyString);
b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
text1.DataBindings.Add(b);
// Bind to the textbox
textBox1.DataBindings.Add(b);
private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
// The method converts only to string type. Test this using the DesiredType.
if(cevent.DesiredType != typeof(string)) return;
// Use the ToString method to format the value as currency ("c").
cevent.Value = ((decimal) cevent.Value).ToString("c");
}
private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
{
// The method converts back to decimal type only.
if(cevent.DesiredType != typeof(decimal)) return;
// Converts the string back to decimal using the static Parse method.
cevent.Value = Decimal.Parse(cevent.Value.ToString(),
NumberStyles.Currency, null);
}
>In VFP, it's easy to setup Input format in textbox controls. Initializing the control.value to zero and putting "999,999.99" to its input properties will let the user input numeric with commas, and will not allow any non-numeric inputs. Why in C# doesn't have this properties? How do you do it guys?
-----------------------------------------
Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.comwww.prenia.comWeblog: blogs.prenia.com/cathi