Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to do Input Formatting in C# controls?
Message
 
To
19/06/2002 06:28:47
Czarina Joyce Villanueva
Innovision Systems International
Manila, Philippines
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00670054
Message ID:
00670154
Views:
46
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.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform