문제

C#을 사용하고 있으며 속성 그리드 컨트롤이 포함 된 Windows 양식이 있습니다.

PropertyGrid의 선택된 보도를 설정 파일에 할당하여 설정을 표시하고 편집 할 수 있습니다. 그러나 설정 중 하나는 비밀번호입니다. 비밀번호 설정의 일반 텍스트 값이 아닌 필드에 별표를 표시하고 싶습니다.

저장하면 필드가 암호화되지만 사용자가 암호에 입력 할 때 별표가 표시된 일반 암호 입력 상자처럼 행동하고 싶습니다.

비밀번호로 표시하기 위해 설정 속성에 적용 할 수있는 속성이 있는지 궁금합니다.

감사.

도움이 되었습니까?

해결책

.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