smr5
smr5

Reputation: 2793

Dynamically add Items to Combobox VB.NET

I have 2 combobox in windows application. I want to programmatically add Items to second combobox based on what is selected in the first combobox.

This is what I do in the selected index change of the combobox:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Try
        If ComboBox1.SelectedIndex = 0 Then
            ComboBox2.Items.Add("MM")
            ComboBox2.Items.Add("PP")
        ElseIf ComboBox1.SelectedIndex = 1 Then
            ComboBox2.Items.Add("SMS")
        ElseIf ComboBox1.SelectedIndex = 2 Then
            ComboBox2.Items.Add("MMS")
            ComboBox2.Items.Add("SSSS")
        End If

    Catch ex As Exception

    End Try
End Sub

It works fine, however, if I keep selecting different items it's keep adding the value over and over. I would like to add those values only once.

Also, when I add an item I would prefer to add an ID with the item description. I tried:

ComboBox2.Items.Add("SSSS", "1")

It seems that it's not working.

Any suggestions?

Upvotes: 2

Views: 55637

Answers (2)

ilans
ilans

Reputation: 2707

Regarding:

"Also, when I add an item I would prefer to add an ID with the item description"

You can use the AddRange function of the ComboBox or make a list as I show here and use it as a datasource - as @Plutonix mentioned in his comment.

With it you can add an array of objects of a type that holds a value and a description, given that you override the ToString base function (of Object class).

Here is such a class:

Public Class CodeAndDescription
    Public code As String
    Public description As String

    Public Overrides Function ToString() As String
        Return Me.code + " - " + description
    End Function
End Class

Now make a list of that upper class and add it to the combobox. Something like:

Dim lstItems As List(Of CodeAndDescription) = GetList()
yourComboBox.Items.Clear()
yourComboBox.Items.AddRange(lstItems.ToArray) 

Don't forget to Cast the retrieved object when you take it out of the combo:

Dim codeDesc As CodeAndDescription = TryCast(yourComboBox.Items(0), CodeAndDescription)

I've done this on a check list, but I think the principle is the same for a ComboBox.

Upvotes: 1

illumi
illumi

Reputation: 458

try this

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Try
        If ComboBox1.SelectedIndex = 0 Then
            If Not (ComboBox2.Items.Contains("MM")) And Not (ComboBox2.Items.Contains("PP")) Then
                ComboBox2.Items.Add("MM")
                ComboBox2.Items.Add("PP")
            End If
        ElseIf ComboBox1.SelectedIndex = 1 Then
            If Not (ComboBox2.Items.Contains("SMS")) Then
                ComboBox2.Items.Add("SMS")
            End If
        ElseIf ComboBox1.SelectedIndex = 2 Then
            If Not (ComboBox2.Items.Contains("MMS")) And Not (ComboBox2.Items.Contains("SSSS")) Then
                ComboBox2.Items.Add("MMS")
                ComboBox2.Items.Add("SSSS")
            End If
        End If

    Catch ex As Exception

    End Try

Upvotes: 2

Related Questions