cascade two comboBoxes

DeadpoolMarv

New member
Joined
Jan 9, 2013
Messages
4
Programming Experience
Beginner
Hello Everybody,

Newbie here. Can you help me with my very simple problem (at least for most of you). I am trying to create 2 combo boxes and the requirement is if I select and item in ComboBox1 selected items in ComboBox2 should only appear (out of 10 items on 5 should show up).

Thank you very much in advance guys how to read your post on this.
 
Hi,

For us to help you better and give a more accurate reply you need to describe how you are initially populating your ComboBox's. Is this done by binding data sources from a data set or are you doing this manually either through code or via the IDE?

Let us know and we will try and help where we can.

Cheers,

Ian
 
Hi,

For us to help you better and give a more accurate reply you need to describe how you are initially populating your ComboBox's. Is this done by binding data sources from a data set or are you doing this manually either through code or via the IDE?

Let us know and we will try and help where we can.

Cheers,

Ian

Thanks for the quick response Sir, I am doing it manually by coding.
 
Hi,

Here is an example of how to do this on the basis that you are coding your values manually. The thing to remember is that to do things like this, there has to be a relationship between the two sets of data so that you know what needs to go in the second ComboBox:-

VB.NET:
Public Class Form1
  Private ParentList() As String = {"Animal Name", "Tree Name", "Flower Name"}
  Private ChildList() As String = {"Animal Name - Pig", "Animal Name - Cow", "Animal Name - Elephant", "Tree Name - Birch", "Tree Name - Elm", "Tree Name - Apple", "Flower Name - Primrose", "Flower Name - Pansie", "Flower Name - BlueBell"}
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ComboBox1.Items.AddRange(ParentList)
  End Sub
 
  Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    ComboBox2.Items.Clear()
    ComboBox2.Items.AddRange(ChildList.Where(Function(x) x.StartsWith(ComboBox1.SelectedItem.ToString)).ToArray)
  End Sub
End Class

Here I have created two lists. The Parent List goes in the first ComboBox and when that ComboBox changes it uses the value of the Parent ComboBox to select the elements that need to be displayed in the second ComboBox.

So that's one way to do it, but a more efficient way of doing things like this is to have your information in some form of Data Source and then bind the controls to the Data Source. To demonstrate this principal, have a look at this post made by jmcilhinney:-

Master/Detail (Parent/Child) Data-binding (.NET 2.0+ WinForms)-VBForums

Hope that helps.

Cheers,

Ian
 
VB.NET:
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

if combobox1.text="one of the picked items" then
combobox2.text="item 1"
else
combobox2.text="item 2"
end if
end sub

Hope it helps :)
 
Last edited:
Back
Top