Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[VB.NET]How we can Create Tree Menu in ComboBox


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


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

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 Sub
6-Select the Combo box before Select event Write few lines.it sets some properties of Combo box.

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

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

If 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.
bump good tutorial in my opinion
PHP & .NET Contributor.
Pretty good. I have been wondering how to do this.
That was good. Yet, If it was so long, You could have some pics here.
(May 7, 2010 07:42 PM)Aeonhack Archivist Wrote:  Pretty good. I have been wondering how to do this.

im glad this helped you...
(May 7, 2010 07:53 PM)jaikrishna Wrote:  That was good. Yet, If it was so long, You could have some pics here.

pictures? what for this is some of the easyes things you can do on here...
PHP & .NET Contributor.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by SмℯґƒMay 13, 2020 11:30 AM 2 128 May 13, 2020 11:30 AM
Last Post: 4Txe4y33C9zvNhMj
[C] by SмℯґƒMay 11, 2020 04:01 PM 0 137 May 11, 2020 04:01 PM
Last Post: Sмℯґƒ
[Shell] by Emre_Sep 1, 2019 07:17 PM 0 134 Sep 1, 2019 07:17 PM
Last Post: Emre_