I have several TextBoxes, which display angular values. I format these using different custom implementations of IFormatProvider, for example: The value 1.5707963267949 can be displayed as 1.5707963267949, 0.5π or 90°. The TextBoxes are one-way-databound and I perform the formatting by handling the Format event of the Binding:
void bindingToMyTextBox_Format(object sender, ConvertEventArgs e)
{
// angleValueFormat is one of a few a custom implementations of
// IFormatProvider performing the actual formatting work.
e.Value = String.Format(this.angleValueFormat, "{0:0.###}", (double)e.Value);
}
I want to allow the user to input values in the same format as they are displayed in. So I need to parse TextBox.Text to obtain the original value 1.5707963267949. Is there any "IParserProvider" interface providing parsing services, like IFormatProvider in the opposite direction? Or would you suggest a different approach?
From what I understand, i can’t use my IFormatProviders in the Convert.ToDouble Method (String, IFormatProvider) because, to determine in which format the number to parse is, it uses only the NumberFormatInfo-Object returned by the IFormatProvider.GetFormat(), which I obviously can’t use to specify the necessary info.
Thank you!
richn
No, that’s not possible. I think mostly because in general it’s not possible to automatically deduce a parser from a formatter (which is, iirc, internally implemented by a method
ICustomFormatter.Format).I would suggest to define your own interface
and implement it in your classes for the
angleValueFormatmamber. You’ll probably have to use some regexes or so to do that.Then you can use
and bind this to your binding’s
Parseevent.