Dear all,
How to show the File Properties in Property Gird
dana
Printable View
Dear all,
How to show the File Properties in Property Gird
dana
Probably best for reuse, create a class with the properties you are wanting to display in the PG. Then populate from the selected file. Then bind the class to the PG using the .SelectedObject property. Using attribute tags on your props designate how and where they are shown in the grid, if at all desired.
VB Code:
Imports System.ComponentModel Public Class PropertyClass Private mprop1 As String = "Text" Private mprop2 As Integer = 1 Private mprop3 As Bitmap Private mprop4 As Color = Color.Red <Category("Category1"), DefaultValue("Text")> _ Public Property Property1() As String Get Return mprop1 End Get Set(ByVal Value As String) mprop1 = Value End Set End Property <Category("Category1"), DefaultValue(1)> _ Public Property Property2() As Integer Get Return mprop2 End Get Set(ByVal Value As Integer) mprop2 = Value End Set End Property <Category("Category2")> _ Public Property Property3() As Bitmap Get Return mprop3 End Get Set(ByVal Value As Bitmap) mprop3 = Value End Set End Property <Category("Category2"), DefaultValue(GetType(Color), "Red")> _ Public Property Property4() As Color Get Return mprop4 End Get Set(ByVal Value As Color) mprop4 = Value End Set End Property End Class
Example Usage:
VB Code:
Private mproperties 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 = mproperties End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Save mproperties content here End Sub
Dear Robert,
Thanks for your reply.Now I found this code do display the default file properties.
vb Code:
PropertyGrid1.SelectedObject = New FileInfo("c:\test.txt")
And now I can add some properties to this additionally
Cool, didnt think about doing it that way but if you want to override some of the default properties then you will need the intermediate class like I posted.
Can we able to add some extra property to inaddition to the default showing when this command is executed
vb Code:
PropertyGrid1.SelectedObject = New IO.FileInfo("filename.ext")
You can only show properties of the object that you've assigned to the SelectedObject property of the grid. If you want to show other properties then you'd have to assign a different type of object that has those properties. You can define your own type that wraps a FileInfo and provides those additional properties if you like.
Dear JMC,
Wheather I have to write a new wrapper for all the properties of the Fileinfo.Or just inherit the existing property and add my properties /
Which do you think would be better and why? Is there any impediment to doing it that way?
I think inheriting the existing properties,adding new properties (mine) and showing is will be the best one :confused: :confused:
What benefit(s) does that provide? Is there any impediment to doing it that way? Is there any benefit to doing it the other way?
I think the question is what are you trying to accomplish? Just showing the default properties, showing and adding new properties, showing and overriding default properties, etc.
Yes Robert,You are 200 :D :D :D % Correct :p