2009-01-14 33 views
12

Tôi đang sử dụng C# và có biểu mẫu cửa sổ có chứa điều khiển lưới thuộc tính.Có thể đánh dấu thuộc tính được hiển thị trong lưới thuộc tính dưới dạng trường mật khẩu

Tôi đã gán SelectedObject của thuộc tính vào tệp cài đặt, hiển thị và cho phép tôi chỉnh sửa cài đặt. Tuy nhiên một trong các cài đặt là mật khẩu - và tôi muốn nó hiển thị dấu hoa thị trong trường thay vì giá trị văn bản thuần túy của cài đặt mật khẩu.

Trường sẽ được mã hóa khi được lưu, nhưng tôi muốn nó hoạt động giống như hộp nhập mật khẩu thông thường có dấu hoa thị được hiển thị khi người dùng nhập mật khẩu.

Tôi tự hỏi nếu có thuộc tính có thể được áp dụng cho thuộc tính thiết lập để đánh dấu nó là mật khẩu?

Cảm ơn.

Trả lời

20

Bắt đầu với Net 2, bạn có thể sử dụng PasswordPropertyTextAttribute thuộc sở hữu mật khẩu của bạn.

Hy vọng điều này sẽ hữu ích.

1

Tôi không nghĩ rằng bạn có thể nhận PropertyGrid để trao đổi với dấu hoa thị, nhưng bạn có lẽ có thể sử dụng một chiều kiểu chuyển đổi và một trình soạn thảo phương thức ... như vậy:

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

Câu trả lời được chấp nhận cho biết bạn có thể (làm những gì OP yêu cầu)! – noelicus

1

Dưới đây là những gì tôi đã làm trong quá khứ. Nó hiển thị "********" cho mật khẩu trong lưới, với nút "..." để cho phép người dùng đặt mật khẩu (sử dụng hộp thoại bạn cung cấp).

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; 
    } 
} 
Các vấn đề liên quan