【问题标题】:Is it possible to mark a property shown in a property grid as a password field是否可以将属性网格中显示的属性标记为密码字段
【发布时间】:2023-03-27 19:09:01
【问题描述】:

我正在使用 C# 并且有一个包含属性网格控件的 Windows 窗体。

我已将属性网格的 SelectedObject 分配给设置文件,该文件显示并允许我编辑设置。但是其中一项设置是密码 - 我希望它在字段中显示星号,而不是密码设置的纯文本值。

该字段在保存时将被加密,但我希望它像普通的密码输入框一样,在用户输入密码时显示星号。

我想知道是否有一个属性可以应用于设置属性以将其标记为密码?

谢谢。

【问题讨论】:

    标签: c# passwords attributes propertygrid


    【解决方案1】:

    从 .Net 2 开始,您可以使用附加到您的密码属性的 PasswordPropertyTextAttribute。

    希望这会有所帮助。

    【讨论】:

    • 请注意,如果您手动将此属性添加到代码中,为了达到预期效果,它必须声明为[PasswordPropertyText(true)],而不仅仅是[PasswordPropertyText]。第二个版本使用默认构造函数,将属性的Password属性设置为false,这意味着属性网格会忽略它。
    【解决方案2】:

    这是我过去所做的。它在网格中显示“********”作为密码,并带有一个“...”按钮以允许用户设置密码(使用您提供的对话框)。

    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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      我不认为您可以让 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"}
                      }
                  }
              });
          }
      }
      

      【讨论】:

      • 接受的答案说你可以(按照 OP 的要求去做)!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多