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
 
 
 
 
 
TitleSelect a particular property in a PropertyGrid control concisely in Visual Basic .NET
DescriptionThis example shows how to select a particular property in a PropertyGrid control concisely in Visual Basic .NET.
KeywordsPropertyGrid, property grid, select property, VB.NET
CategoriesControls, Software Engineering, VB.NET
 
Thanks to Malcolm S Powell.

The PropertyGrid control provides access to the properties it represents but not in a very friendly way. It holds them in a tree structure. The exact structure depends on whether the user is displaying items by category or alphabetically.

The find_item function starts at the currently selected item and follows parent links up until it reaches the root of the tree. It then calls function find_node to search the tree for the property with a given name.

 
' Find the root item. Then search the property grid's
' item tree for the indicated item.
Private Function find_item(ByVal property_grid As _
    PropertyGrid, ByVal item_label As String) As GridItem
    Dim root As GridItem = property_grid.SelectedGridItem
    While Not (root.Parent Is Nothing)
        root = root.Parent
    End While
    Return find_node(item_label, root)
End Function
 
Function find_node recursively searches a PropertyGrid item's children for the target label. If it finds the target item, it returns the node containing it. Otherwise the function returns Nothing.
 
' Search the property grid's
' item tree for the indicated item.
Private Function find_node(ByVal item_label As String, _
    ByVal root As GridItem) As GridItem
    If root.Label = item_label Then
        Return root
    Else
        For Each gi As GridItem In root.GridItems
            Dim result As GridItem = find_node(item_label, _
                gi)
            If Not (result Is Nothing) Then Return result
        Next gi
        Return Nothing
    End If
End Function
 
Subroutine SelectPropertyGridItem uses function find_item to find the desired item. It then sets that item as the PropertyGrid's selected item.
 
' Select ths property grid's indicated item.
Public Sub SelectPropertyGridItem(ByVal property_grid As _
    PropertyGrid, ByVal item_label As String)
    property_grid.SelectedGridItem = _
        find_item(property_grid, item_label)
End Sub
 
See also Select a particular property in a PropertyGrid control in Visual Basic .NET.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated