Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleScroll a PropertyGrid control to make a particular property visible in Visual Basic .NET
DescriptionThis example shows how to scroll a PropertyGrid control to make a particular property visible in Visual Basic .NET.
KeywordsPropertyGrid, scroll, property
CategoriesControls, VB.NET
 
The PropertyGrid control's SelectedGridItem property is an object that describes the property that is currently selected in the property grid. While SelectedGridItem is settable, the PropertyGrid control does not give you much access to the objects representing the othuer properties.

The objects representing the properties are arranged in a tree. The exact shape of the tree depends on whether the PropertyGrid is displaying properties by category or alphabetically.

An object in the tree has a Parent property that leads to the object's parent in the tree. It also has a nodes collection that leads to the object's children in the tree.

To set the selected property, this FindGridItem function starts at the currently selected item. It follows Parent links until it finds the root of the tree. It then traverses the tree until it finds an item with a specific label. This is the name of a property (you will run into trouble if a property has the same name as a category, for example, Appearance). It returns the object it found.

 
' Find the GridItem with the given label.
Private Function FindGridItem(ByVal item_label As String) _
    As GridItem
    ' Find the GridItem root.
    Dim root As Object _
        'System.Windows.Forms.PropertyGridInternal.SingleSelectRootGridEntry
    root = Me.PropertyGrid1.SelectedGridItem
    Do Until root.Parent Is Nothing
        root = root.Parent
    Loop

    ' Search the tree.
    Dim nodes As New Collection
    nodes.Add(root)
    Do Until nodes.Count = 0
        Dim test_node As Object = nodes(1)
        nodes.Remove(1)
        If test_node.Label = item_label Then Return _
            test_node

        For Each obj As Object In test_node.GridItems
            nodes.Add(obj)
        Next obj
    Loop

    Return Nothing
End Function
 
The program uses FindGridItem to find the object representing a particular property. If that object is not Nothing, the program sets the PropertyGrid's SelectedGridItem property to it. The PropertyGrid control automatically scrolls if necessary to make that property visible.
 
Private Sub btnItem_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles _
    btnCellPhone.Click, btnLastName.Click, btnZip.Click
    Dim btn As Button = DirectCast(sender, Button)
    Dim grid_item As GridItem = FindGridItem(btn.Text)
    If Not (grid_item Is Nothing) Then _
        Me.PropertyGrid1.SelectedGridItem = grid_item
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated