是否有可能标志着一个属性网格作为密码字段中显示的属性
-
22-07-2019 - |
题
我使用C#和具有包含属性网格控制一个视窗形式。
我已分配PropertyGrid中的SelectedObject到一个设置文件,其中显示,让我编辑设置。然而的设置之一是密码 - 我想它显示在字段星号,而不是密码设置的明文值
保存时的字段将被加密,但我希望它表现得像用星号正常密码输入框中显示,当用户在口令输入。
我不知道是否存在能够被施加到设置属性,将其标记为是密码的属性
感谢。
解决方案
与.net 2开始,可以使用 PasswordPropertyTextAttribute 附着到您的密码属性。
希望这有助于。
其他提示
下面是我在过去所做的那样。它显示“********”用于在网格中的密码,用“...”按钮,以允许用户设置的密码(使用您提供的对话框)。
public class User
{
[TypeConverter(typeof(PasswordConverter))]
[Editor(typeof(PasswordEditor), typeof(UITypeEditor))]
public string Password { get; set; }
}
public class PasswordConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string)) return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
string password = (string)value;
if (password != null && password.Length > 0)
{
return "********";
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
public class PasswordEditor : UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
string password = (string)value;
// Show a dialog allowing the user to enter a password
return password;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
}
我不认为你可以得到的PropertyGrid交换到星号,但你也许可以用单向型转换器和一个模式编辑器...像这样:
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
class Foo
{
[TypeConverter(typeof(PasswordConverter))]
[Editor(typeof(PasswordEditor), typeof(UITypeEditor))]
public string Password { get; set; }
// just to show for debugging...
public string PasswordActual { get { return Password; } }
}
class PasswordConverter : TypeConverter
{
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
{
return destinationType == typeof(string) ? "********" :
base.ConvertTo(context, culture, value, destinationType);
}
}
class PasswordEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService svc = (IWindowsFormsEditorService)
provider.GetService(typeof(IWindowsFormsEditorService));
if (svc != null) {
TextBox tb;
Button btn;
Form frm = new Form { Controls = {
(tb = new TextBox { PasswordChar = '*', Dock = DockStyle.Top,
Text = (string)value}),
(btn = new Button { Text = "OK", Dock = DockStyle.Bottom, DialogResult = DialogResult.OK})
}, AcceptButton = btn};
if (frm.ShowDialog() == DialogResult.OK)
{
value = tb.Text;
}
}
return value;
}
}
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form {
Controls = {
new PropertyGrid {
Dock = DockStyle.Fill,
SelectedObject = new Foo { Password = "Bar"}
}
}
});
}
}
不隶属于 StackOverflow