Using the Properties Grid

Mick Dugan

Freshman
Joined
Jun 15, 2003
Messages
45
In my application, users may create a number of objects. Information about these objects (height, width, name, etc) is stored in several 2 dimensional arrays. How do I go about using the properties grid to display the data?
 
A search on Google yielded this as the first result.

The tutorial implements the ICustomTypeDescriptor interface to customize what is shown in the PropertyGrid.

Depending on your needs, it might be easier to create a class that exposes the property you need to be edited.
C#:
class EditableItem {
    // Constructor
    public EditableItem(int width, int height, int etCetera){
        _Width = width;
        _Height = height;
        _EtCetera = etCetera;
    }

    // Expose data to edit as properties
    int _Height;
    public int Height{ get{ return _Height;} set {_Height = value;} }
 
    int _Width;
    public int Width{ get{ return _Width;} set {_Width = value;} }
 
    int _EtCetera;
    public int EtCetera{ get{ return _EtCetera;} set {_EtCetera = value;} }
}
 
public void EditItem(int index1, int index2) {
    // Create an object with the data to edit
    EditableItem i = new EditableItem(
        Heights[index1, index2], 
        Widths[index1, index2],
        EtCeteras[index1, index2]);
 
    // Present the user with the data in the property grid
    MyPropertyGrid.SelectedObject = i;
    
    // At some point you will need to save the previously selected item's data
    // back to the arrays. A good time to do this would be immediately before
    // a new item is selected.
}
Visual Basic:
Public Class EditableItem
    'Constructor
    Public Sub New (width As Integer, height As Integer, etCetera As Integer)
        _Width = width
        _Height = height
        _EtCetera = etCetera
    End Sub
 
    'Expose data to edit as properties
    Dim _Width As Integer
    Property Width() As Integer
        Get
            Return _Width
        End Get
        Set(ByVal value As Integer)
            _Width = value
        End Set
    End Property
 
    Dim _Height As Integer
    Property Height() As Integer
        Get
            Return _Height
        End Get
        Set(ByVal value As Integer)
            _Height = value
        End Set
    End Property
 
    Dim _EtCetera As Integer
    Property EtCetera() As Integer
        Get
            Return _EtCetera
        End Get
        Set(ByVal value As Integer)
            _EtCetera = value
        End Set
    End Property
End Class
 
Public Sub EditItem(ByVal index1 As Integer, ByVal index2 As Integer)
    'Create an object with the data to edit
    Dim i As New EditableItem( _
        Heights(index1, index2), _
        Widths(index1, index2), _
        EtCeteras(index1, index2))
 
    'Present the user with the data in the property grid
    MyPropertyGrid.SelectedObject = i
 
    'At some point you will need to save the previously selected item's data
    'back to the arrays. A good time to do this would be immediately before
    'a new item is selected.
End Sub
 
Back
Top