c#PropertyGrid怎么修改属性错误的提示信息
图中“经纬度”输入的格式类似于控件的“Size”属性,但是当我输入“q”的时候弹出对话框,对话框中“详细信息”显示“类型‘System.String’。。。”总觉得不好,因为客户看不懂。
我的问题是:能不能修改属性窗口错误提示中的“详细信息”,当然也可以自定义弹出窗口显示并且将值恢复到先前正确的值,“经纬度”是我自定义的类
public struct 经纬度
{
[DefaultValueAttribute(true)]
public decimal 经度 { get; set; }
[DefaultValueAttribute(false)]
public decimal 纬度 { get; set; }
}
下面是“经纬度”对应的类型转换器
public class SpellingOptionsConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context,
System.Type destinationType)
{
if (destinationType == typeof(经纬度))
return true;
return base.CanConvertTo(context, destinationType);
}
//这个函数将输入的值转化为 经度+“,”+纬度 显示在“经纬度”上,类似于Size属性
public override object ConvertTo(ITypeDescriptorContext context,CultureInfo culture,
object value, System.Type destinationType)
{
if (destinationType == typeof(System.String) &&
value is 经纬度)
{
经纬度 so = (经纬度)value;
return so.经度 + "," + so.纬度;
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
try
{
string s = (string)value;
int colon = s.IndexOf(',');
if (colon != -1)
{
string 经度 = s.Substring(0, colon);
string 纬度 = s.Substring(colon + 1);
经纬度 temp = new 经纬度();
temp.经度 = Convert.ToDecimal(经度);
temp.纬度 = Convert.ToDecimal(纬度);
return temp ;
}
}
catch
{
return value;
}
}
return value;
}
}
[解决办法]
好吧,想复杂了
那就更简单了
public class SpellingOptionsConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context,
System.Type destinationType)
{
if (destinationType == typeof(经纬度))
return true;
return base.CanConvertTo(context, destinationType);
}
//这个函数将输入的值转化为?经度+“,”+纬度?显示在“经纬度”上,类似于Size属性
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
object value, System.Type destinationType)
{
try
{
if (destinationType == typeof(System.String) &&
value is 经纬度)
{
经纬度 so = (经纬度)value;
return so.经度 + "," + so.纬度;
}
return base.ConvertTo(context, culture, value, destinationType);
}
catch
{
throw new ArgumentException("卖个萌");
}
}
public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
try
{
if (value is string)
{
string s = (string)value;
int colon = s.IndexOf(',');
if (colon != -1)
{
string 经度 = s.Substring(0, colon);
string 纬度 = s.Substring(colon + 1);
经纬度 temp = new 经纬度();
temp.经度 = Convert.ToDecimal(经度);
temp.纬度 = Convert.ToDecimal(纬度);
return temp;
}
}
throw new ArgumentException("卖个萌");
}
catch
{
throw new ArgumentException("卖个萌");
}
}
}