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 to Create Dependent ComboBox in VB.Net

Author: DevASP
Download Source Code : 448_dependent_comboboxes.zip

In this simple article you will learn that how can you dependent ComboBox in VB.Net.

Simply just create New Project in VB.net. Drag three Combo Box controls from tool box and place on form. Now Write code in form load event:

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

        Me.BackColor = Color.LightGray

        Me.Text = "DevAsp"

 

        ComboBox1.Items.Add("1-First")

        ComboBox1.Items.Add("2-Second")

        ComboBox1.Items.Add("3-Third")

 

End Sub

 

Now write code on Selected change event of combo box 1 and 2

 

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

 

        ComboBox2.Items.Clear()

 

        If ComboBox1.SelectedIndex = 0 Then

 

            ComboBox2.Items.Add("1.1-First")

            ComboBox2.Items.Add("1.2-Second")

            ComboBox2.Items.Add("1.3-Third")

        End If

 

        If ComboBox1.SelectedIndex = 1 Then

            ComboBox2.Items.Add("2.1-First")

            ComboBox2.Items.Add("2.2-Second")

            ComboBox2.Items.Add("2.3-Third")

        End If

 

        If ComboBox1.SelectedIndex = 2 Then

            ComboBox2.Items.Add("3.1-First")

            ComboBox2.Items.Add("3.2-Second")

            ComboBox2.Items.Add("3.3-Third")

        End If

 

End Sub

 

 

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

 

        Dim str As String = ComboBox2.SelectedItem

        ComboBox3.Items.Clear()

        If str.Substring(0, 3) = "1.1" Then

            ComboBox3.Items.Add("1.1.1-First")

            ComboBox3.Items.Add("1.1.2-Second")

            ComboBox3.Items.Add("1.1.3-Third")

        End If

 

        If str.Substring(0, 3) = "1.2" Then

            ComboBox3.Items.Add("1.2.1-First")

            ComboBox3.Items.Add("1.2.2-Second")

            ComboBox3.Items.Add("1.2.3-Third")

        End If

 

        If str.Substring(0, 3) = "1.3" Then

            ComboBox3.Items.Add("1.3.1-First")

            ComboBox3.Items.Add("1.3.2-Second")

            ComboBox3.Items.Add("1.3.3-Third")

        End If

 

        If str.Substring(0, 3) = "2.1" Then

            ComboBox3.Items.Add("2.1.1-First")

            ComboBox3.Items.Add("2.1.2-Second")

            ComboBox3.Items.Add("2.1.3-Third")

        End If

 

        If str.Substring(0, 3) = "2.2" Then

            ComboBox3.Items.Add("2.2.1-First")

            ComboBox3.Items.Add("2.2.2-Second")

            ComboBox3.Items.Add("2.2.3-Third")

        End If

 

        If str.Substring(0, 3) = "2.3" Then

            ComboBox3.Items.Add("2.3.1-First")

            ComboBox3.Items.Add("2.3.2-Second")

            ComboBox3.Items.Add("2.3.3-Third")

        End If

 

        If str.Substring(0, 3) = "3.1" Then

            ComboBox3.Items.Add("3.1.1-First")

            ComboBox3.Items.Add("3.1.2-Second")

            ComboBox3.Items.Add("3.1.3-Third")

        End If

 

        If str.Substring(0, 3) = "3.2" Then

            ComboBox3.Items.Add("3.2.1-First")

            ComboBox3.Items.Add("3.2.2-Second")

            ComboBox3.Items.Add("3.2.3-Third")

        End If

 

        If str.Substring(0, 3) = "3.3" Then

            ComboBox3.Items.Add("3.3.1-First")

            ComboBox3.Items.Add("3.3.2-Second")

            ComboBox3.Items.Add("3.3.3-Third")

        End If

    

End Sub

Article Comments
THIS WAS SUPER USEFULL! THANK YOU!!!

Posted on 8/1/2009 12:41:37 AM by Sean McCarthy

Just Superbe

Posted on 1/25/2010 7:19:54 AM by Aditya Sarkar

Excellent... Simple and useful.

Posted on 4/2/2010 10:55:16 AM by Pravin

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Inheritance in Visual Basic.Net (Part 2)

Disclaimer - Privacy
© 2002-2017 DevASP.net