PropertyGrid Problems


Dec 8, 2004
My own little world
Programming Experience
I'm hoping that there's someone here who has some extensive experience with the PropertyGrid control.... I've been trying to figure out this problem for the past 4 days, and all I'm hitting is roadblocks....

I'm writing an editor for some developers, and attempting to use the propgrid as a nice enhancement to my editor... I came across TypeConverters when I was trying to figure out how to make expandable property lists from my classes... the ExpandableObjectConverter gave me (seemingly) what I was looking for as shown in the following diagram:


you'll notice the color and size properties which branch off into their components... this is EXACTLY what I wanted... until i tried doing it with a class that had other classes as objects... I wanted these nested classes to behave the same way and expand... but there was no + sign on the propertygrid beside them.... I was devastated! All of my work had amounted to nothing!! and just when I had gotten my hopes up...

What I wanted was more like the following... which I've seen in programs using the property grid before... and am quite confused on how to achieve:
| |----Red
| |----Green
| \----Blue
| |----Red
| |----Green
| \----Blue

Note the fast that there are multiple layers to the tree... this is what I want... and I've managed to acheive when I stick classes from the .NET framework into my primary class as properties..... my question is this: why do my classes not act the same way, and how do I make them act the same way?? ...

The folowing is the TypeConverter I'm applying to my guiLocation class:
Friend Class guiLocationConverter : Inherits ExpandableObjectConverter
			Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
				If (sourceType Is GetType(String)) Then Return True
				Return MyBase.CanConvertFrom(context, sourceType)
			End Function

			Public Overloads Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
				If TypeOf value Is String Then
						Dim s As String = CType(value, String)
						Dim versionParts() As String
						Dim VersionString As String = ""
						versionParts = Split(s, ",")
						If Not IsNothing(versionParts) Then
							Dim _GuiLocation As GuiLocation = New GuiLocation
							If Not IsNothing(versionParts(0)) Then _GuiLocation.Left = versionParts(0)
							If Not IsNothing(versionParts(1)) Then _GuiLocation.Top = versionParts(1)
							Return _GuiLocation
						End If
					Catch ex As Exception
						Throw New ArgumentException("Can not convert '" + value + "' to type guiLocation")
					End Try
				End If
				Return MyBase.ConvertFrom(context, culture, value)
			End Function

			Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
				If (destinationType Is GetType(System.String) AndAlso TypeOf value Is GuiLocation) Then
					Dim _GuiLocation As GuiLocation = CType(value, GuiLocation)
					Return _GuiLocation.Left & "," & _GuiLocation.Top
				End If
				Return MyBase.ConvertTo(context, culture, value, destinationType)
			End Function

			Public Overloads Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
				If (destinationType Is GetType(GuiLocation)) Then Return True
				Return MyBase.CanConvertFrom(context, destinationType)
			End Function
		End Class
And here's the location class:
<TypeConverter(GetType(guiLocationConverter))> _
	Public Class GuiLocation
		Private mvarTop As Integer
		Private mvarLeft As Integer
		Public Property Top() As Integer
				Return mvarTop
			End Get
			Set(ByVal Value As Integer)
				mvarTop = Value
			End Set
		End Property
		Public Property Left() As Integer
				Return mvarLeft
			End Get
			Set(ByVal Value As Integer)
				mvarLeft = Value
			End Set
		End Property
	End Class

If ANYONE has any experience with this type of thing, can you please help me out?? I'm stumped this is an important project.... thanks
I understand the gist of what you're trying to accomplish but I'm confused by the above code. It seems you are rewriting the Point class already provided and calling X Left and Y Top. Won't the standard Point class suffice? If not then you could replace '<TypeConverter(GetType(guiLocationConverter))> _' at the beginning of the class with '<TypeConverter(GetType(ExpandableObjectConverter))> _' and when you use the class as a property in another class, the property explorer should show a plus just as the standard Point class does (for example the location property of a control).

Also, are you meaning to have color represented by Red, Green, and Blue components?

I think you're trying to make this more complicated than it is by using custom TypeConverters, as I did once :).
heh... well yah... that was just a test though... I've got many classes that need to be converted to work with the propertygrid control... that was just an example...

I've also already tried '<TypeConverter(GetType(ExpandableObjectConverter))> _' and it has the same limitations that my code seem to... which is unfortunate... it would have been nice if MS had made this a little easier >_<

one of my classes that needs converting is represented here:
	ColorTopLeft (D3DColorValue)
		Red (Single)
		Green (Single)
		Blue (Single)
		Alpha (Single)
	ColorTopRight (D3DColorValue)
	ColorBottomLeft (D3DColorValue)
	ColorBottomRight (D3DColorValue)
	Picture (GuiPicture)
		Size (guiSize)
			Width (Integer)
			Height (Integer)
		wasLoaded (Boolean)
		Texture (Integer)
		FileName (String)
Last edited:
ergh... nevermind... as always, I've messed something up on my own...
I started a new project, called it "test", implemented some new classes, and it worked like a charm... god knows why I couldn't get it working in my old project... I've really gotta learn the KISS rule... (keep it simple stupid :p)