I finally foud the property grid thanks to Aaron Young in this thread, but I
am having some trouble implementing it on my user control. Does anyone
know how it works?
Thanks in advance for any assistance.
Printable View
I finally foud the property grid thanks to Aaron Young in this thread, but I
am having some trouble implementing it on my user control. Does anyone
know how it works?
Thanks in advance for any assistance.
What exactly are you having trouble doing?
All you should have to do is set the SelectedObject property to the control/class you want to set properties for.
the property grid control is ONLY for displaying properties of objects(programming objects).
If you were expecting to be able to just do a PropertyGrid1.AddItem("ItemName", "Value", "Category") you will be disappointed. :(
You could decompile the control....
It's not as bad as it sounds, all you'd have to do is create a class that is comprised of the Properties you want to offer/manipulate, i.e.
VB Code:
Imports System.ComponentModel Public Class PropertyClass Private m_prop1 As String = "Text" Private m_prop2 As Integer = 1 Private m_prop3 As Bitmap Private m_prop4 As Color = Color.Red <Category("Category1"), DefaultValue("Text")> _ Public Property Property1() As String Get Return m_prop1 End Get Set(ByVal Value As String) m_prop1 = Value End Set End Property <Category("Category1"), DefaultValue(1)> _ Public Property Property2() As Integer Get Return m_prop2 End Get Set(ByVal Value As Integer) m_prop2 = Value End Set End Property <Category("Category2")> _ Public Property Property3() As Bitmap Get Return m_prop3 End Get Set(ByVal Value As Bitmap) m_prop3 = Value End Set End Property <Category("Category2"), DefaultValue(GetType(Color), "Red")> _ Public Property Property4() As Color Get Return m_prop4 End Get Set(ByVal Value As Color) m_prop4 = Value End Set End Property End Class
Example Usage:
VB Code:
Private m_properties As New PropertyClass Private propGrid As New PropertyGrid Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load propGrid.Location = New Point(10, 10) propGrid.Size = New Size(200, 250) propGrid.Dock = DockStyle.Fill propGrid.CommandsVisibleIfAvailable = True propGrid.Text = "My Property Grid" pnlPropGrid.Controls.Add(propGrid) propGrid.SelectedObject = m_properties End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Save m_properties content here End Sub
Ok, but I am having trouble with it when I try to integrate it to a form for my
user control. In my control I have a property - Panels (collection) that I am
trying to get the elipsis to appear and when that is clicked it will bring up my
property form with the property grid on it and a listbox to add/remove panels.
Nowe I need each entry of the listbox to have its own properties and as you
click each one the properties will change accordingly.
I have another thread where I am trying to get the elipsis and hyperlink text
for an About link in the description for my control.
Thats all :rolleyes: nothing complicated :eek:
You need to create a custom Collection Editor, here's an example:
MyPanel Class:
MyPanels Class:VB Code:
Imports System.ComponentModel Public Class MyPanel Private m_name As String = "" Private m_prop1 As String = "" <Browsable(True), DefaultValue("")> _ Public Property Name() As String Get Return m_name End Get Set(ByVal Value As String) m_name = Value End Set End Property <Browsable(True), DefaultValue("")> _ Public Property Property1() As String Get Return m_prop1 End Get Set(ByVal Value As String) m_prop1 = Value End Set End Property Public Overrides Function ToString() As String Return m_prop1 End Function End Class
PanelCollectionEditor Class:VB Code:
Public Class MyPanels Inherits CollectionBase Default Public ReadOnly Property Item(ByVal index As Integer) As MyPanel Get Return List(index) End Get End Property Public Function Add(ByVal value As MyPanel) As Integer Return List.Add(value) End Function Public Sub Remove(ByVal value As MyPanel) List.Remove(value) End Sub Public Function Contains(ByVal value As MyPanel) As Boolean Return List.Contains(value) End Function Public Function IndexOf(ByVal value As MyPanel) As Integer Return List.IndexOf(value) End Function End Class
MyControl Class:VB Code:
Public Class PanelCollectionEditor Inherits System.ComponentModel.Design.CollectionEditor Private m_id As Integer = 1 Public Sub New(ByVal type As System.Type) MyBase.New(type) End Sub Protected Overrides Function CreateNewItemTypes() As Type() ' Return an Array of Types this editor can manage Dim types(0) As Type types(0) = GetType(MyPanel) Return types End Function Protected Overrides Function CreateInstance(ByVal itemType As Type) As Object ' Create a new Panel instance Dim panel As MyPanel panel = MyBase.CreateInstance(itemType) panel.Name = "Panel" & m_id m_id += 1 Return panel End Function Protected Overrides Function SetItems(ByVal editValue As Object, ByVal value() As Object) ' Load the list of Panels into the Editor Dim panel As MyPanel Dim panels As MyPanels panels = editValue For Each panel In value panels.Add(panel) Next Return editValue End Function End Class
Example:VB Code:
Imports System.ComponentModel Public Class MyControl Private m_panels As New MyPanels Private m_text As String = "Something" <Bindable(True), DefaultValue("Something")> _ Public Property Text() As String Get Return m_text End Get Set(ByVal Value As String) m_text = Value End Set End Property <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _ Editor(GetType(PanelCollectionEditor), GetType(System.Drawing.Design.UITypeEditor))> _ Public ReadOnly Property Panels() As MyPanels Get Return m_panels End Get End Property End Class
VB Code:
Private m_control As New MyControl Private propGrid As New PropertyGrid Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load propGrid.Location = New Point(10, 10) propGrid.Size = New Size(200, 250) propGrid.Dock = DockStyle.Fill propGrid.CommandsVisibleIfAvailable = True propGrid.Text = "My Property Grid" pnlPropGrid.Controls.Add(propGrid) propGrid.SelectedObject = m_control End Sub
Thank you Aaron for going to the trouble and detail for me. I will try implementing it tonight.
:thumb: :thumb:
I didnt get to it last night, so hopefully tonight. I just wanted to post a link that is
relevant to the thread in case anyone has this issue with the PG. :thumb:
Ok, I got a little farther until I hit this. Everytime I click the elipsis button in my test project
and add a new panel to the UIEditor and click ok, when I click it a second time to bring up the editor
again it adds another duplicate panel item to the collection. I can not remove them either.
I know it has to do with the MyPanels class code perhaps?
*BUMP*
Try this instead, it seems I made a mistake in my first attempt at a custom Collection Editor, this seems to work the way you want:
VB Code:
Public Class PanelCollectionEditor Inherits System.ComponentModel.Design.CollectionEditor Private m_id As Integer = 1 Public Sub New(ByVal type As System.Type) MyBase.New(type) End Sub Protected Overrides Function CreateNewItemTypes() As Type() ' Return an Array of Types this editor can manage Dim types(0) As Type types(0) = GetType(MyPanel) Return types End Function Protected Overrides Function CreateInstance(ByVal itemType As Type) As Object ' Create a new Panel instance Dim panel As MyPanel panel = MyBase.CreateInstance(itemType) panel.Name = "Panel" & m_id m_id += 1 Return panel End Function End Class
Thanks, I'll give it a try.
Quick question: I am having trouble with the Panel class. Seems like I need a nested collection of items
in each Panel class. I dont know if this is the correct design but I dont se how to have settings for
the items in each Panel?
I have a small thread on the start of it here.
Ok I found a control that will be a good example of what panels, Panel, Items, and Item I need to duplicate
for my control.
The listview control has an Items collection and when you click on it, that brings up the Items editor.
Then you can add a item and for each item you add you have a subitems collection that is
for each item. When you click the subitems collection you get another popup SubItems editor. Basically it
has two editors that are daisy chained off of each other.
So I need a Panels collection editor where I can add/remove a Panel. Then for each Panel I would have a Items
collection editor where I could add/remove Items for each Panel.
Hope I didnt confuse anyone :D
Ok, I added another procedure to the editor -
Only problem so far is that when an item is removed from the collection and a new items it added,VB Code:
Protected Overrides Sub DestroyInstance(ByVal instance As Object) Dim panel As MyPanel MyBase.DestroyInstance(m_id) End Sub
it doesnt pickup from the number that was left off. For ex. If I created three panels and removed the third one (Panel3),
then added back another one, it will continue at Panel4 instead of Panel3. :confused:
It does this with or without the DestroyInstance procedure also. :confused:
I also found this method - MyBase.CreateCollectionForm. Would this be for when you need to display a
secondary nested collection for a collection item?
If this is too much then what about creating my own Editor which I could add the functionality
that I dont know how to duplicate?