I just thought the following conversation could be useful to others as well.
The .NET PropertyGrid Control is a wonderful control which enables you to load any object (or set of objects) in it and displays/exposes all the public properties of the object through reflection. The Framework class library provides various Attributes and Classes to cutomize the way your proerties look. Generally, when you need to customize the way your properties look in the property Grid, either you use the TypeConverters or you create a new Cusotm Editor for your properties. Let me explain this further,
No.1 – Custom Editors for Properties
You must have used a Collection Editor, if your class contains a collection property then visual studio shows a button in-front of the colle ction property and clicking that opens a new dialog in which you can add/edit collection items.
No.2 – Using TypeConverters for Properties
You must have seenn a Font Property in various controls, this property as you see is expandable and then with all the combination of the selected values you can actually show the Font Property, also you must have seen the Icon Property which shows an Icon Image in-front of the property or there are certain properties which show a drop-down in-front of property, these properties use the TypeConverters. There are various type converters available in System.ComponentModel namespace.
I have written some code using the type converters which nearly solves your problem, but the problem with the type converters is that they always update the property after the value has been entered, I mean you won’t be able to update it character by character as you want in your case. You will only be able to convert your property value into ‘**********’ after teh full property has been entered. Consider you have the following class which contains public properties which are to be shown in the property Grid.
public class NewClass
private string _text;
//Add this Attribute to Specify the TypeConverter
public string Text
_text = value;
Look above and see the Attribute that I am using on the Property, this attribute specifies that the CusotmPassConeverter, is the converter to be used for this property. Let me implement the CusotmPassConverter now.
public class CustomPassConverter : System.ComponentModel.StringConverter
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
string passValue = (string) value;
string pass = new string(‘*’,passValue.Length);
Now when you will load this class in the PropertyGrid, and edit the value of the property, after editing the value your value will be changed in ‘***********’.
I have also tried using the UITypeEditor, which is one of the editors which allows you to draw in the Property and has a PaintValue method, but it has the same problem that it modifies the property after it has been edited.
IF AN EXACT SOLUTION IS POSSIBLE
I beleive that I can always go the old Win32 way. I can write a CustomPropertyGrid Control by inheriting from the original PropertyGrid control overrideing its WndProc, searching for messages and then can do anything. BUT this solution will not work in case for Visual Studio as it is not using my Custom Control.
Yes the correct solution is possible in one way, you can write a custom editor dialog like the Collection Editor Dialog we talked ablut above, let the user enter the Password in that custom dialog (Collection Editor also allows that). Actually when you write a separate dialog as an editor you have complete control over it. So its better to Implement a Custom Dialog. I’ll try to write this if I get some extra time.
Senior Software Engineer
KalSoft (Pvt) Limited
fakhar siddiqui <firstname.lastname@example.org> wrote:
Its me Fakhar. I am facing some problem in C# when using “Property Grid”. If u people used Property Grid then u obviously know the properties which we define in our class are mapped to Property Grid after storing the object of that class in “PropertyGrid” .
The Problem is that I have property in my Class named “Password”. I want when user will edit that field in property grid it should display “*” instead of the character which is typed. I think attributes in namespace [using System.ComponentModel] may help. But I could not find any solution.
If you can help me in this regard, I will appreciate it.
Thanks You Very Much.