Author: DevASP
Tutorial: Crow407
In this tutorial I Will Explain How we can Create Tree Menu Which is Embeded in ComboBox using VB.Net.
This UserControl is a ComboBox control that has its dropdown window replaced with a TreeView control. Handling the mouse, the keyboard and the focusing was a bit tricky and the control is not 100% perfect but it works just fine. Two issues are unresolved, but they were not important to me: firstly, since the TreeView is a child control it cannot be drawn outside the form; and secondly, a ComboBox.CloseUp call must be placed on the Form.Click event since the control does not monitor its own parent. For diligent people it should be relatively easy to adjust this control to their own needs, and even to replace the TreeView control with something else and build other types of specialized ComboBoxes.
To create Combo box with Tree Menu Follow Some Steps and Understand Code Below.
1. Create new application Using File menu and Choose vb.net project.
Now you will see the form Open in your application.
2. Now enter the routine below Against the load event of form. This routine infact Set some initial TreeView properties.Creating the tree nodes while the TreeView is visible avoids the bug that shows a horizontal scrollbar on the bottom of the tree.
Code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBoxTree1.TreeView.ShowLines = False
ComboBoxTree1.SuspendLayout()
ComboBoxTree1.TreeView.Visible = True
Dim Node As TreeNode = ComboBoxTree1.TreeView.Nodes.Add("--- No selection ---")
Node.ImageIndex = 2
Node.SelectedImageIndex = 2
ComboBoxTree1.TreeView.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node0", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node1", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node2"), New System.Windows.Forms.TreeNode("Node3")}), New System.Windows.Forms.TreeNode("Node4"), New System.Windows.Forms.TreeNode("Node5")})})
ComboBoxTree1.TreeView.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node0", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node1", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node2"), New System.Windows.Forms.TreeNode("Node3")}), New System.Windows.Forms.TreeNode("Node4"), New System.Windows.Forms.TreeNode("Node5")})})
ComboBoxTree1.TreeView.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node0", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node1", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node2"), New System.Windows.Forms.TreeNode("Node3")}), New System.Windows.Forms.TreeNode("Node4"), New System.Windows.Forms.TreeNode("Node5")})})
ComboBoxTree1.TreeView.Visible = False
Node = ComboBoxTree1.TreeView.Nodes.Add("New node...")
Node.ImageIndex = 3
Node.SelectedImageIndex = 3
ComboBoxTree1.ResumeLayout()
' Select a node...
Me.ComboBoxTree1.SelectedNode = Me.ComboBoxTree1.TreeView.Nodes.Item(2).Nodes.Item(1)
End Sub3. Now select the click event of form the auto generated routine is appear below.Write the line of code this line actually this should be handled by the ComboBoxTree itself, but the current version of the control does not handle this.
Code
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Me.ComboBoxTree1.DroppedDown = False
End Sub4. now double click on button the auto generated routine is appear.Write the code below which check Selected node is exist or not.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Code
If Me.ComboBoxTree1.SelectedNode Is Nothing Then
MsgBox("No tree node is selected.")
Else
MsgBox(Me.ComboBoxTree1.SelectedNode.Text)
End If
End Sub5. Select the Combo box after Select event Write few lines.it sets some properties of Combo box.
Code
Private Sub ComboBoxTree1_AfterSelect(ByVal sender As Object) Handles ComboBoxTree1.AfterSelect
If Me.ComboBoxTree1.SelectedNode Is Nothing Then
Me.TextBox1.Text = "No tree node is selected."
ElseIf Me.ComboBoxTree1.SelectedNode.Text.StartsWith("---") Then
Me.ComboBoxTree1.SelectedNode = Nothing
Else
Me.TextBox1.Text = Me.ComboBoxTree1.SelectedNode.Text
End If
End SubCode
Private Sub ComboBoxTree1_BeforeSelect(ByVal sender As Object, ByVal Node As System.Windows.Forms.TreeNode, ByRef Cancel As Boolean) Handles ComboBoxTree1.BeforeSelect
If (Not Node Is Nothing) AndAlso Node.Text.StartsWith("New") Then
Cancel = True
Me.ComboBoxTree1.DroppedDown = False
MsgBox(Node.Text)
End If
End SubCode
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.ComboBoxTree1.SelectedNode = Nothing
End SubIf You Like this tutorial, please check out my other tutorials by clicking ==> H3R3
[img]http://i685.photobucket.com/albums/vv215/blackpearl4/THANKS.gif[/img]
PHP & .NET Contributor.


