我有一个带有标准树控件的 VB.NET winforms 应用程序。树控件每五秒刷新一次,我希望这样如果选择了一个节点,那么它会在刷新后被选中。这是我的代码(m_oSelectedNode 是一个全局树节点):
Public Sub SaveLabelNodes()
m_cLabelTVNodes.Clear()
If tvLabels.Nodes.Count = 0 Then
Return
End If
m_LabelScrollPos = GetTreeViewScrollPos(tvLabels)
m_oSelectedNode = tvLabels.SelectedNode
m_bLabelRootExpanded = tvLabels.Nodes(0).IsExpanded
For Each node As TreeNode In tvLabels.Nodes(0).Nodes
Dim oNode As New NodeInfo
oNode.bExpanded = node.IsExpanded
oNode.szNodeName = node.Text
m_cLabelTVNodes.Add(oNode)
Next
End Sub
Public Sub RestoreLabelNodes()
tvLabels.BeginUpdate()
Dim nIndex As Integer = 0
If m_bLabelRootExpanded Then
tvLabels.Nodes(0).Expand()
End If
For Each oNode As NodeInfo In m_cLabelTVNodes
If FindNode(tvLabels, oNode) Then
If oNode.bExpanded Then
tvLabels.Nodes(0).Nodes(nIndex).Expand()
End If
End If
nIndex += 1
Next
If Not m_oSelectedNode Is Nothing Then
tvLabels.SelectedNode = m_oSelectedNode
End If
SetTreeViewScrollPos(tvLabels, m_LabelScrollPos)
tvLabels.EndUpdate()
End Sub
树控件将滚动到最后一个位置,但最后一个选择不会选择!