Quantcast
Channel: Manjuke's Blog
Viewing all articles
Browse latest Browse all 56

Show properties of a class on Property Grid

$
0
0

When developing user controls, usually we use Booleans, Integers, Strings, etc.. as data types of the attributes of the control. But sometimes, we have to use structures or other classes as attributes. So when we use those, we should be able to change or browse the attributes of those structures or classes.

Open visual studio IDE and create a new Windows Forms Application type project.

Add a new class and name it as ‘MyCustomClass’. And define two properties. One integer and a string type.

publicclass MyCustomClass {

publicint MyIntProperty { get; set; }
publicstring MyStringProperty { get; set; }

publicoverridestring ToString() {
return"...";
}
}







**Please note that I have overridden the ‘ToString’ method. Because this what will be shown on the property grid when the properties are collapsed.



Now add a new user control to the project and name it as ‘MyCutomUsercontrol’. And create three properties. One integer, string and MyCustomClass. And use ‘[TypeConverter(typeof(ExpandableObjectConverter))]’ attribute on the third property. The syntax should be:






publicpartialclass MyCutomUsercontrol : UserControl {
public MyCutomUsercontrol() {
InitializeComponent();
}

private MyCustomClass _MyCustomClass = new MyCustomClass();

publicint Property1 { get; set; }
publicstring Property2 { get; set; }

[TypeConverter(typeof(ExpandableObjectConverter))]
[EditorBrowsable(EditorBrowsableState.Always)]
public MyCustomClass Property3 {
get {
return _MyCustomClass;
}
set {
_MyCustomClass = value;
}
}

}





 



Now build the solution. And add it a windows form. And on the property grid you can see your custom controls properties.



screen_1


Viewing all articles
Browse latest Browse all 56

Latest Images

Trending Articles



Latest Images