日期:2014-05-19  浏览次数:20792 次

请问:propertygrid中如何将密码显示为*******?
查了一下网络,有方案是在GET{}中只返回*****字符串,不返回实际密码;但如此处理的话,界面显示是能解决了,问题是内部程序处理的时候,要访问读取这个属性又比较罗嗦了,请问有什么好的解决方案吗?

------解决方案--------------------
public class PasswordStringConverter: StringConverter//System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return base.CanConvertFrom (context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if( destinationType.GetType() == typeof(string) )
return true;

return base.CanConvertTo (context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if( value.GetType() == typeof(string) )
{
int stringSize;
string retVal = "* ";
Random randomString = new Random();


if( value != null )
stringSize = ((string)value).Length;
else
stringSize = randomString.Next( 10 );

for( int i = 0; i < stringSize; i++ )
retVal += "* ";

return retVal;
}

return base.ConvertTo (context, culture, value, destinationType);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return false;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
string[] standardValues = new string[1];
int stringSize;
string retVal = "* ";
Random randomString = new Random();


stringSize = randomString.Next( 10 );

for( int i = 0; i < stringSize; i++ )
retVal += "* ";

standardValues[0] = retVal;

return new StandardValuesCollection( standardValues );
}
}

/// <summary>
/// Summary description for MyTextbox.
/// </summary>
public class MyTextbox : System.Windows.Forms.TextBox
{
[TypeConverter(typeof(PasswordStringConverter))]
public new string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
}
------解决方案--------------------
PasswordStringConverter class is from:
http://www.codeproject.com/cs/miscctrl/PropertyGridDataFormattin.asp