Ist es möglich, eine Eigenschaft gezeigt in einem Eigenschaftenraster als Kennwortfeld zu markieren

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

Frage

Ich bin mit C # und haben ein Fenster bilden eine Eigenschaft Grid Control enthält.

Ich habe die SelectedObject der Property zu einer Einstellungsdatei zugeordnet, die anzeigt, und lässt mich die Einstellungen bearbeiten. Jedoch eine der Einstellungen ist ein Passwort -. Und ich möchte es Sternchen im Feld angezeigt werden, anstatt der Klartextwert der Passwort-Einstellung

wird das Feld verschlüsselt werden, wenn gespeichert, aber ich will es mit Sternchen wie ein normales Passwort-Eingabefeld verhalten angezeigt, wenn der Benutzer in dem Kennwort eingeben.

Ich frage mich, ob es ein Attribut, das auf die Einstellung Eigenschaft angewandt werden kann, um es zu markieren, wie ein Kennwort zu sein?

Danke.

War es hilfreich?

Lösung

Beginnend mit .Net 2 können Sie die PasswordPropertyTextAttribute an Ihr Passwort Eigenschaft.

Hope, das hilft.

Andere Tipps

Hier ist, was ich in der Vergangenheit getan haben. Es zeigt „********“ für das Passwort im Netz, mit einer Schaltfläche „...“, damit der Benutzer das Passwort setzen (mit einem Dialog, den Sie angeben).

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

Ich glaube nicht, Sie Property bekommen können Sternchen zu tauschen, aber man könnte vielleicht einen One-Way-Typ-Wandler und einen modalen Editor ... wie so verwenden:

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"}
                }
            }
        });
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top