Combobox listing

softhard

Active member
Joined
Sep 29, 2012
Messages
35
Programming Experience
Beginner
Hello,
I have two comboboxes and each have some strings inside them. if the user select one string in one combobox should list respective items in other combobx. Is there any good way to do it? I am new to VB.net!
 
Hi,

Here is an example of how to populate the contents of a second combo box based on the value of a first combo box.

Create a new form, add two combo boxes and then replace the code behind with the below.

VB.NET:
Public Class Form1
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ComboBox1.Items.Add("a")
    ComboBox1.Items.Add("b")
    ComboBox1.Items.Add("c")
    ComboBox1.Items.Add("d")
    ComboBox1.Items.Add("e")
    ComboBox2.Enabled = False
  End Sub
 
  Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim strWords As String() = {"able", "Arable", "Born", "Bored", "cheese", "chaff", "change", "delta", "done"}
    Dim NewComboList As IEnumerable(Of String) = (From Words In strWords Where Words.Substring(0, 1) = ComboBox1.SelectedItem Select Words)
 
    With ComboBox2
      .SelectedIndex = -1
      .Items.Clear()
    End With
    For Each Word In NewComboList
      ComboBox2.Items.Add(Word)
    Next
    ComboBox2.Enabled = True
  End Sub
End Class
Obviously you need to adapt these combo boxes to accommodate your own code and data sources but the principle will be the same although the selection criteria for combo box 2 will be different depending on what you are trying to do.

Good Luck,

Ian
 
Hello,
In the given code, it is listing only based on the first letter. It is not working in m case even i arrange the strings.

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Dim strWords As String() = {"(MBZ) Read coils(0x01)", "(MBZ) Read discrete inputs(0x02)", "MBZ Read holding registers(0x03)", "MBZ Read input registers(0x04)", "MBZ Write single coil(0x05)", "MBZ Write single register(0x06)", "MBZ Write multiple coils(0x0F)", "MBZ Write multiple registers(0x10)", "RUP Mating request(0x00)", "RUP Mating reply(0x01)", "RUP Temperature(0x02)", "RUP Button press(0x03)", "RUP Button press ACK(0x04)", "MSP Mating announcement(0x00)", "MSP Slave data(0x01)", "MSP Heating output(0x02)", "NDP Ping(0x00)", "NDP Pong(0x01)"}
Dim NewComboList As IEnumerable(Of String) = (From Words In strWords Where Words.Substring(0, 3) = ComboBox2.SelectedItem Select Words)
With ComboBox3
.SelectedIndex = -1
.Items.Clear()
End With
For Each Word In NewComboList
ComboBox3.Items.Add(Word)
Next
ComboBox3.Enabled = True
End Sub

Private Sub Serilcom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
Me.ComboBox2.Items.AddRange(New Object() {"(MBZ)Modbus over ZCP", "(RUP)Room Unit protocol", "(MSP)Master Slave protocol", "(NDP)Node discovery protocol"})
ComboBox3.Enabled =
False
End Sub
 
It's up to you to work out the appropriate condition to determine whether a child value is included or not. This is your condition:
VB.NET:
Where Words.Substring(0, 3) = ComboBox2.SelectedItem
If it's not working then your condition is wrong. Think about what you've got there. You're saying that a value should only be included if it's first three characters is equal to what's selected in the ComboBox. Given that every value in the ComboBox is longer than three characters, that can never be True for any child value so no child value will ever be shown.
 
I know that and now i used case structure to list out my combobox data. it is working fine. But, i am curious to know this model.

Thank you.
 
Like I said, use a condition that works and the code will work. You might use String.StartsWith or String.Contains but, whatever you use, just like any code, it has to be a valid implementation of the condition you want to test.
 
Hi,

To follow on from jmcilhinney's comments see below how you would have done it with your own data examples.

As jmcilhinney tried to explain the important thing here is the where statement of the LINQ query. LINQ being a Visual Studio based query language which can be used against any object type to specify the returned list of items based on a conditional where clause.

If you forget the LINQ structure for the moment and just concentrate on the where clause then what jmcilhinney was trying to tell you was that you had to match the right portion of data in the first combo box to that portion of data in each string of your strWords array. i.e. you have to match "MBZ" in the first combo box string to that of the strings in the strWords array.

I accomplish this with the following:-

Where Words.Contains(ComboBox2.SelectedItem.ToString.Substring(1, 3))

Hope that helps to explain things for you.

Cheers,

Ian

VB.NET:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available 
    Me.ComboBox2.Items.AddRange({"(MBZ)Modbus over ZCP", "(RUP)Room Unit protocol", "(MSP)Master Slave protocol", "(NDP)Node discovery protocol"})
    ComboBox3.Enabled = False
  End Sub
 
  Private Sub ComboBox2_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    Dim strWords As String() = {"(MBZ) Read coils(0x01)", "(MBZ) Read discrete inputs(0x02)", "MBZ Read holding registers(0x03)", "MBZ Read input registers(0x04)", "MBZ Write single coil(0x05)", "MBZ Write single register(0x06)", "MBZ Write multiple coils(0x0F)", "MBZ Write multiple registers(0x10)", "RUP Mating request(0x00)", "RUP Mating reply(0x01)", "RUP Temperature(0x02)", "RUP Button press(0x03)", "RUP Button press ACK(0x04)", "MSP Mating announcement(0x00)", "MSP Slave data(0x01)", "MSP Heating output(0x02)", "NDP Ping(0x00)", "NDP Pong(0x01)"}
    Dim NewComboList As IEnumerable(Of String) = (From Words In strWords Where Words.Contains(ComboBox2.SelectedItem.ToString.Substring(1, 3)) Select Words)
 
    With ComboBox3
      .SelectedIndex = -1
      .Items.Clear()
    End With
    For Each Word In NewComboList
      ComboBox3.Items.Add(Word)
    Next
    ComboBox3.Enabled = True
  End Sub
 
Back
Top