jamesG77296 jamesG77296 - 5 months ago 20
Vb.net Question

removing items in ComboBox when the similar item is selected Vb.net DesktopApp

i have two combo boxes like this

enter image description here

So for instance if i select

in the first Drop Box the second Drop Box shouldn't show the item

so i am wondering can this be done ?
i am developing a Desktop application using VB.Net

Handles ComboBox1.DropDown
With ComboBox1.Items

End With


You could do it as follows:

    Public Class Form3

            Dim _items() As String = {"Apple", "Orange", "Banana"}

            Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                    ComboBox2.DataSource = _items
                    ComboBox1.DataSource = _items
            End Sub

            Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
                    ComboBox2.DataSource = _items.Where(Function(item) item <> CStr(ComboBox1.SelectedItem)).ToList()
            End Sub

    End Class

On load we setup the datasources. Then when ComboBox1 has a new item selected we filter the list.