Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How we can Create Tree Menu in ComboBox Using VB.Net

Author: DevASP
Download Source Code : 551_ComboBoxTree Demo.zip

In this article 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.

 

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 Sub

 

3. 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.

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click

        Me.ComboBoxTree1.DroppedDown = False

End Sub

 

4. 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

        If Me.ComboBoxTree1.SelectedNode Is Nothing Then

            MsgBox("No tree node is selected.")

        Else

            MsgBox(Me.ComboBoxTree1.SelectedNode.Text)

        End If

    End Sub

 

5. Select the Combo box after Select event Write few lines.it sets some properties of Combo box.

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 Sub

 

6-Select the Combo box before Select event Write few lines.it sets some properties of Combo box.

 

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 Sub

 

7-Now double click on button the auto generated routine is appear. Write the code below which Assign nothing to Selected node Selected node .

 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Me.ComboBoxTree1.SelectedNode = Nothing

    End Sub

Now Built and run the Project Your You will see the Tree menu in the combo box.The source Code is given by this article for your Complete Understanding. Just run it you can understand how easily we can create it.  

Article Comments
How to Add Combox,Check box to datagrid control in vb.net using sql as backend

Posted on 1/8/2007 6:29:49 AM by devaraj

how to export sql data to excel using vb.net code

Posted on 1/8/2007 6:38:04 AM by r.devaraj

i want to get code with C# ! i am not familer vb.net
if you don't mind ,please send code to me ,thank you !

Posted on 5/10/2010 9:26:48 PM by AlexTfiiy

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Stream Handling Using Visual Basic.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net