2009-01-14 18 views
12

parola alanı olarak işaretlemek mümkün mü? C# kullanıyorum ve bir özellik ızgarası denetimi içeren bir windows formunu kullanıyorum.Özellik kılavuzunda gösterilen bir özelliği

Özellik kılavuzunun SelectedObject öğesini, ayarları görüntüleyen ve düzenlememi sağlayan bir ayar dosyasına atadım. Ancak ayarlardan biri şifre - ve parola ayarının düz metin değeri yerine alandaki yıldız işaretlerini göstermesini istiyorum.

Bu alan kaydedildiğinde şifrelenecek, ancak kullanıcının parola girdiğinde yıldız işareti olan normal bir parola giriş kutusu gibi davranmasını istiyorum.

Parola olarak işaretlemek için ayar özelliğine uygulanabilecek bir özellik olup olmadığını merak ediyorum?

Teşekkürler.

cevap

1

Sana yıldızlarla için takas PropertyGrid alabilirsiniz sanmıyorum ama belki şöyle bir tek yönlü tip dönüştürücü ve kalıcı bir editör ... kullanabilirsiniz : Burada

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"} 
       } 
      } 
     }); 
    } 
} 
+0

Kabul edilen cevap yapabileceğinizi (OP'nin ne istediğini yapıyor) söyler! – noelicus

1

ne var ben Geçmişte yaptım. Paroladaki parola için "********" ifadesini, kullanıcının şifreyi (sağladığınız bir iletişim kutusunu kullanarak) ayarlamasına izin veren bir "..." düğmesiyle gösterir.

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; 
    } 
} 
İlgili konular