Formatting doesn't work

M

Michael Wong

I am trying to format a textbox (to display it as percentage) using the
following code (taken from the help sample), but it doesn't work and I found
the faulty line but don't know what to do with it.

private BindDiscount()
{
Binding b = new Binding("Text", this.dsInventory1, "Table.Discount");
b.Format += new ConvertEventHandler(DoubleToPercentageString); // this line
makes all other controls blank
b.Parse += new ConvertEventHandler(PercentageStringToDouble);
tbDiscount.DataBindings.Add(b);
}

private void DoubleToPercentageString(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 percentage ("p").
cevent.Value = ((double)cevent.Value).ToString("p");
}

private void PercentageStringToDouble(object sender, ConvertEventArgs
cevent)
{
// The method converts back to double type only.
if(cevent.DesiredType != typeof(double)) return;

// Converts the string back to double using the static Parse method.
cevent.Value = Double.Parse(cevent.Value.ToString(),NumberStyles.Any,
null);
}
 
M

Miha Markic

Hi Michael,

Check if there is an exception raised within DoubleToPercentageString event
or try to comment some of its lines and see what happens.
(It should work just fine)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top