[RESOLVED] Displaying Property Arrays in PropertyGrid control
Hello.
It seems as if the propertygrid control cannot display property arrays. Is this the case?
When I do this :
Code:
Imports System.ComponentModel
Public Class comPicturePirate
Private intAmountImages As Integer
Private strWebURL As String
Private strPageName As String
Private strPageTitle As String
Public lstOutPutImages As New List(Of Image)
Public Sub New(ByVal pic As List(Of Image))
For index = 0 To pic.Count '- 1
' Me.PB = pbn
lstOutPutImages.Item(index) = pic.Item(index)
Next
End Sub
<Description("Contained Images"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property Images(ByVal index As Integer) As Image
Get
Return lstOutPutImages.Item(index)
End Get
Set(ByVal value As Image)
lstOutPutImages.Add(value)
End Set
End Property
<Description("Website URL Where Image(s) Should Appear"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property WebURL(ByVal index As Integer) As String
Get
Return strWebURL(index)
End Get
Set(ByVal value As String)
strWebURL = value
End Set
End Property
<Description("Page Name Where Image(s) Should Appear"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property PageName(ByVal index As Integer) As String
Get
Return strPageName(index)
End Get
Set(ByVal value As String)
strPageName = value
End Set
End Property
<Description("Page Name Where Image(s) Should Appear"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property PageTitle(ByVal index As Integer) As String
Get
Return strPageTitle(index)
End Get
Set(ByVal value As String)
strPageTitle = value
End Set
End Property
<Description("Number Of Images Contained"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property AmountImages() As Integer
Get
Return intAmountImages
End Get
Set(ByVal value As Integer)
intAmountImages = value
End Set
End Property
End Class
And in the calling form I do this :
Code:
Private Sub btnPPCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPPCreate.Click
Dim frmProps As New frmPPProperties
Private arrOutPutImages As comPicturePirate
arrOutPutImages = New comPicturePirate
frmProps.prgPPProp.CommandsVisibleIfAvailable = True
frmProps.prgPPProp.Text = "Images"
frmProps.prgPPProp.SelectedObject = arrOutPutImages
frmProps.Show()
End Sub
It only shows the property named AmountImages - which is not a property array.
I need to show the other propertyies as well, and I do not know how
Can anyone help please?
Re: Displaying Property Arrays in PropertyGrid control
You should be able to notice that the only one that is showing, is the only one that does not require a parameter.
Public Property PageTitle() As String()
That is returning a property array, not 1 entry of a instance, the property editor can not get at 'strPageTitle' variable to edit it, only the instance of the string that you have attempted to return, you need to give it the whole thing, and then the editor will replace the whole thing. You can make custom editors, good example here.
btw, you do realise you are returning the byte at a specified location in the string, not an entry from a string array?
Re: Displaying Property Arrays in PropertyGrid control
Thank you, it seems like I will need to make something similar as the one you have suggested.
Give me some time, and I'll let you know how it goes.
Thank you again
Re: Displaying Property Arrays in PropertyGrid control
I did this :
I broke the one class with all my properties into 2 separate classes. The inner class inherits from the main class.
This is what I did :
Code:
Imports System.ComponentModel
Public Class comPicturePirate
Private intAmountImages As Integer
<Description("Number Of Images Contained"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property AmountImages() As Integer
Get
Return intAmountImages
End Get
Set(ByVal value As Integer)
intAmountImages = value
End Set
End Property
End Class
Code:
Imports System.ComponentModel
Public Class comPicturePirateInner
Inherits comPicturePirate
Private strWebURL As String
Private strPageName As String
Private strPageTitle As String
Public lstOutPutImage As Image
Sub New()
End Sub
<Description("Contained Images"), _
Category("Settings")> _
Public Property Image() As Image
Get
Return lstOutPutImage
End Get
Set(ByVal value As Image)
lstOutPutImage = value
End Set
End Property
<Description("Website URL Where Image(s) Should Appear"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property WebURL() As String
Get
Return strWebURL
End Get
Set(ByVal value As String)
strWebURL = value
End Set
End Property
<Description("Page Name Where Image(s) Should Appear"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property PageName() As String
Get
Return strPageName
End Get
Set(ByVal value As String)
strPageName = value
End Set
End Property
<Description("Page Name Where Image(s) Should Appear"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property PageTitle() As String
Get
Return strPageTitle
End Get
Set(ByVal value As String)
strPageTitle = value
End Set
End Property
End Class
And on the form I did :
Code:
Private arrOutPutImages As comPicturePirateInner
Private Sub picPPPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picPPPreview.Click
Dim frmProps As New frmPPProperties
frmProps.prgPPProp.CommandsVisibleIfAvailable = True
frmProps.prgPPProp.Text = "Images"
frmProps.prgPPProp.SelectedObject = arrOutPutImages
arrOutPutImages.Image = New Bitmap(lstPPImages.SelectedItem.ToString())
arrOutPutImages.PageName = ""
arrOutPutImages.PageTitle = ""
arrOutPutImages.WebURL = ""
frmProps.Show()
End Sub
Thank you
Re: Displaying Property Arrays in PropertyGrid control
I'm not sure why you have that layout, surely 'comPicturePirateInner' should be 1 image so you an instance of this class for every image. You then add those classes to a 'List(Of comPicturePirateInner)' as a property of the 'comPicturePirate' class, it should not inherit from it.
Re: Displaying Property Arrays in PropertyGrid control
Thanks for your additional advice! I'm not sure I understand what I must do. What I have done, seems to work, but now I'm having problems saving the property settings. I have tried My.Settings, but it just saves one setting for all pictures. So, if your solution is more likely to help me in this regard, maybe, with your help, I can try to give it a go :)
Re: Displaying Property Arrays in PropertyGrid control
Think about how this is done throughout the .NET Framework. There is a class that represents an item. There is another class that represents a collection of those items. There is a class that represents an object and it has a property whose type is the collection. You'll see this pattern over and over throughout the .NET Framework. The DataTable class has Rows and Columns properties that are types DataRowCollection and DataColumnCollection and contain DataRow and DataColumn objects respectively. The Form has a Controls property that is type ControlCollection and contains Control objects. Etc, etc.
So, you should define a class that has a property for each value you want to store. Let's name that class Picture. It will have an Image property, etc. You should then define a class that will represent a collection of Picture objects, which takes basically no code:
vb.net Code:
Public Class PictureCollection
Inherits System.Collections.ObjectModel.Collection(Of Picture)
End Class
All the standard collection functionality is inherited so you don;t need any code unless you want custom behaviour.
Now, you define a class to represent some other type that has a property that is a PictureCollection object:
vb.net Code:
Public Class SomeType
Private _pictures As New PictureCollection
Public ReadOnly Property Pictures() As PictureCollection
Get
Return Me._pictures
End Get
End Property
End Class
Now, just as you do for so many types in the .NET Framework, you can create an instance of the SomeType class and then add to and remove from its Pictures collection, e.g.
vb.net Code:
Dim someObject As New SomeType
someObject.Pictures.Add(New Picture(...))
MessageBox.Show(someObject.Pictures.Count.ToString(), "Picture Count")
In the designer, a SomeType object will have a Pictures property in the Properties window. You would create a custom designer for that that allows you to add Picture objects to it, much as you add items to a ListView or the like.
Re: Displaying Property Arrays in PropertyGrid control
You're a genius!!
Thank you! Now with my component, I have everything I need!
Re: Displaying Property Arrays in PropertyGrid control
OK slight problem.
I did what you suggested by jmcilhinney. I created a Claas Library project and added these classes to it :
Code:
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Public Class Pictures
Private intImageIndex As Integer
Private strWebURL As String
Private strPageName As String
Private strPageTitle As String
Public lstOutPutImage As Image
<Description("Number Of Images Contained"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property ImageIndex() As Integer
Get
Return intImageIndex
End Get
Set(ByVal value As Integer)
intImageIndex = value
End Set
End Property
<Description("Contained Images"), _
Category("Settings")> _
Public Property Image() As Image
Get
Return lstOutPutImage
End Get
Set(ByVal value As Image)
lstOutPutImage = value
End Set
End Property
<Description("Website URL Where Image(s) Should Appear"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property WebURL() As String
Get
Return strWebURL
End Get
Set(ByVal value As String)
strWebURL = value
End Set
End Property
<Description("Page Name Where Image(s) Should Appear"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property PageName() As String
Get
Return strPageName
End Get
Set(ByVal value As String)
strPageName = value
End Set
End Property
<Description("Page Name Where Image(s) Should Appear"), _
Category("Settings"), Browsable(True), Bindable(True)> _
Public Property PageTitle() As String
Get
Return strPageTitle
End Get
Set(ByVal value As String)
strPageTitle = value
End Set
End Property
End Class
Code:
Public Class PictureCollection
Inherits System.Collections.ObjectModel.Collection(Of Pictures)
End Class
Code:
Public Class PicturePirateOutput
Private _pictures As New PictureCollection
Public ReadOnly Property Pictures() As PictureCollection
Get
Return Me._pictures
End Get
End Property
End Class
The last class (PicturePirateOutput), is a component class.
Now, I am able to add this control to the toolbox, and add it to a form in another project. The problem is that it doesn't save all the settings. As soon as I exit the project, all the entered settings in this component is gone. Is there any trick I need to know in order for the settings to be saved always?
Thank you
Re: Displaying Property Arrays in PropertyGrid control
OK. I have tried saving the property values with My.Settings - to no avail. I cannot save anything. Everything works fine as it should, but it doesn't persist anything. I am giving up :(
It doesn't even create a User.config file in Local Settings\Application Data\ where it should. It does nothing.
Can anyone help, please, I'm really getting despondent now :(
Re: Displaying Property Arrays in PropertyGrid control
You will need to use the DesignerSerializationVisibility attribute on the collection property. This attribute controls the way the property is 'serialized' to the designer code file (Form.Designer.vb file). By default only the top-level property is saved, in your case the properties of the instance of your PictureCollection class will be saved (that class has on properties at the moment so nothing is saved). If you want the properties of the actual items IN that collection to be saved as well, you need to tell that to the designer using the DesignerSerializationVisibility by setting it to 'Content'. This tells the designer that the property has contents that needs to be saved as well (the individual pictures and their properties).
All you need is change your property to this:
Code:
Public Class PicturePirateOutput
Private _pictures As New PictureCollection
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Pictures() As PictureCollection
Get
Return Me._pictures
End Get
End Property
End Class
(You will probably need to add an import statement (System.ComponentModel I think) to the top of the file if it does not recognize the attribute, but you can let VS do that automatically by clicking on the little 'button' that appears beside the error.
Re: Displaying Property Arrays in PropertyGrid control
Something so small, makes such a big difference. Wow, thank you Nick! It works now like a charm!!! :)
I learnt a lot from you guys, the last couple of days - thanks everyone!