هل من الممكن أن علامة خاصية تظهر في خاصية الشبكة مثل حقل كلمة المرور

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

سؤال

أنا باستخدام C# و قد نموذج windows التي تحتوي على خاصية التحكم الشبكة.

وقد كلفت SelectedObject من propertygrid إلى إعدادات الملف الذي يعرض يتيح لي تحرير الإعدادات.ومع ذلك واحدة من إعدادات كلمة المرور - و أود أن عرض العلامات النجمية في الميدان بدلا من النص العادي قيمة إعداد كلمة المرور.

الحقل سوف تكون مشفرة عند حفظها ، ولكن أريد أن تتصرف مثل العادي إدخال كلمة المرور مربع مع النجمة عرضها عندما يقوم المستخدم بإدخال كلمة المرور.

أنا أتساءل عما إذا كان هناك سمة التي يمكن تطبيقها على تحديد الملكية بمناسبة انه بأنها كلمة المرور ؟

شكرا

هل كانت مفيدة؟

المحلول

بدءا .صافي 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