|
|
Title | Select a particular property in a PropertyGrid control in Visual Basic .NET |
Description | This example shows how to select a particular property in a PropertyGrid control in Visual Basic .NET. |
Keywords | PropertyGrid, property grid, select property, VB.NET |
Categories | Controls, Software Engineering, VB.NET |
|
|
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 FindPropertyGridItem function starts at the currently selected item and follows parent links up until it reaches the root of the tree. It then searches the tree for the property with a given name.
|
|
' Find the GridItem with the given label in this
' PropertyGrid.
Public Function FindPropertyGridItem(ByVal property_grid As _
PropertyGrid, ByVal item_label As String) As GridItem
' Find the GridItem root.
Dim root As GridItem
root = property_grid.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 GridItem = DirectCast(nodes(1), _
GridItem)
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
|
|
Subroutine SelectPropertyGridItem uses the FindPropertyGridItem function to find the indicated grid item. It then sets the PropertyGrid control's SelectedGridItem property to that item so it is selected.
|
|
' Select ths property grid's indicated item.
Public Sub SelectPropertyGridItem(ByVal property_grid As _
PropertyGrid, ByVal item_label As String)
property_grid.SelectedGridItem = _
FindPropertyGridItem(property_grid, item_label)
End Sub
|
|
You can use this routine to help validate values displayed in a PropertyGrid. When you click the example program's OK button, the following code checks each of the properties displayed by the PropertyGrid. Function PropertyIsBlank checks each property. If a property is blank, it displays a message to the user, selects the property, and sets focus on the PropertyGrid so the user can immediately type a new value.
|
|
' Verify that the properties are not blank.
Private Sub btnOk_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnOk.Click
If PropertyIsBlank("City", m_Person.City) Then Exit Sub
If PropertyIsBlank("FirstName", m_Person.FirstName) _
Then Exit Sub
If PropertyIsBlank("LastName", m_Person.LastName) Then _
Exit Sub
If PropertyIsBlank("State", m_Person.State) Then Exit _
Sub
If PropertyIsBlank("Street", m_Person.Street) Then Exit _
Sub
If PropertyIsBlank("Zip", m_Person.Zip) Then Exit Sub
MessageBox.Show("Ok", "Ok", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub
Private Function PropertyIsBlank(ByVal property_name As _
String, ByVal property_value As String) As Boolean
If (property_value Is Nothing) OrElse _
(property_value.Length < 1) Then
MessageBox.Show(property_name & " is blank", _
"Blank " & property_name, _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
SelectPropertyGridItem(prgPerson, property_name)
prgPerson.Select()
Return True
Else
Return False
End If
End Function
|
|
See also Select a particular property in a PropertyGrid control concisely in Visual Basic .NET.
|
|
|
|
|
|