Articles in this section
Category / Section

How to display TreeView as the dropdown of the ComboBox in XPMenus in the WinForms XPToolBar?

1 min read

Display TreeView as the dropdown of the ComboBox inXPMenus

The XP Menus framework provides the flexibility to add detached toolbars through CommandBar that can host any .NET control. So, to achieve this we can add the Treeview control as the PopupControl of the ComboBoxDropDown and it can be added to the CommandBar of the XPMenus.

C#

private TreeNode FindNode(TreeNodeCollection nodes, string text)
{
   foreach (TreeNode child in nodes)
      if (child.Text == text)
         return child;
   foreach (TreeNode child in nodes)
   {
      TreeNode matched = this.FindNode(child.Nodes, text);
      if (matched != null)
         return matched;
   }
return null;
}
private void comboDropDown1_DropDown(object sender, EventArgs e)
{
   // Before the drop-down is shown, select a TreeNode based on the text in the combo.
   if (this.comboDropDown1.Text != String.Empty)
   {
      TreeNode matchedNode = this.FindNode(this.treeView1.Nodes, this.comboDropDown1.Text);
      this.treeView1.SelectedNode = matchedNode;
   }
}

VB

Private Function FindNode(ByVal nodes As TreeNodeCollection, ByVal text As String) As TreeNode
   For Each child As TreeNode In nodes
       If child.Text = text Then
          Return child
       End If
   Next child
   For Each child As TreeNode In nodes
       Dim matched As TreeNode = Me.FindNode(child.Nodes, text)
       If matched IsNot Nothing Then
          Return matched
       End If
   Next child
   Return Nothing
End Function
Private Sub comboDropDown1_DropDown(ByVal sender As Object, ByVal e As EventArgs) Handles comboDropDown1.DropDown
   '' Before the drop-down is shown, select a TreeNode based on the text in the combo.
   If Me.comboDropDown1.Text <> String.Empty Then
      Dim matchedNode As TreeNode = Me.FindNode(Me.treeView1.Nodes, Me.comboDropDown1.Text)
      Me.treeView1.SelectedNode = matchedNode
   End If
End Sub

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied