Browse Source

MaskedTextBox: fixed FormatException with numeric masks

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
d83b0cd6bb
  1. 36
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MaskedTextBox/Implementation/MaskedTextBox.cs

36
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/MaskedTextBox/Implementation/MaskedTextBox.cs

@ -337,32 +337,44 @@ namespace Microsoft.Windows.Controls
}; };
} }
private bool _convertExceptionOccurred = false;
private object ConvertTextToValue(string text) private object ConvertTextToValue(string text)
{ {
object convertedValue = null; object convertedValue = null;
Type dataType = ValueType; Type dataType = ValueType;
string valueToConvert = MaskProvider.ToString().Trim(); string valueToConvert = MaskProvider.ToString().Trim();
if (valueToConvert.GetType() == dataType || dataType.IsInstanceOfType(valueToConvert)) try
{ {
convertedValue = valueToConvert; if (valueToConvert.GetType() == dataType || dataType.IsInstanceOfType(valueToConvert))
} {
convertedValue = valueToConvert;
}
#if !VS2008 #if !VS2008
else if (String.IsNullOrWhiteSpace(valueToConvert)) else if (String.IsNullOrWhiteSpace(valueToConvert))
{ {
convertedValue = Activator.CreateInstance(dataType); convertedValue = Activator.CreateInstance(dataType);
} }
#else #else
else if (String.IsNullOrEmpty(valueToConvert)) else if (String.IsNullOrEmpty(valueToConvert))
{ {
convertedValue = Activator.CreateInstance(dataType); convertedValue = Activator.CreateInstance(dataType);
} }
#endif #endif
else if (null == convertedValue && valueToConvert is IConvertible) else if (null == convertedValue && valueToConvert is IConvertible)
{
convertedValue = Convert.ChangeType(valueToConvert, dataType);
}
}
catch
{ {
convertedValue = Convert.ChangeType(valueToConvert, dataType); //if an excpetion occurs revert back to original value
_convertExceptionOccurred = true;
return Value;
} }
return convertedValue; return convertedValue;
@ -373,6 +385,12 @@ namespace Microsoft.Windows.Controls
if (value == null) if (value == null)
value = string.Empty; value = string.Empty;
if (_convertExceptionOccurred)
{
value = Value;
_convertExceptionOccurred = false;
}
//I have only seen this occur while in Blend, but we need it here so the Blend designer doesn't crash. //I have only seen this occur while in Blend, but we need it here so the Blend designer doesn't crash.
if (MaskProvider == null) if (MaskProvider == null)
return value.ToString(); return value.ToString();

Loading…
Cancel
Save