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);
}
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);
}