プロパティグリッドに表示されるプロパティをパスワードフィールドとしてマークすることは可能ですか?

StackOverflow https://stackoverflow.com/questions/443411

質問

C#を使用しており、プロパティグリッドコントロールを含むWindowsフォームがあります。

propertygridのSelectedObjectを設定ファイルに割り当てました。設定ファイルが表示され、設定を編集できます。ただし、設定の1つはパスワードです。パスワード設定のプレーンテキスト値ではなく、フィールドにアスタリスクを表示したいと思います。

フィールドは保存時に暗号化されますが、ユーザーがパスワードを入力するときにアスタリスクが表示される通常のパスワード入力ボックスのように動作するようにします。

パスワードとしてマークするために設定プロパティに適用できる属性があるかどうか疑問に思っていますか?

ありがとう。

役に立ちましたか?

解決

.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"}
                }
            }
        });
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top