jamesG77296 jamesG77296 - 3 months ago 14
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

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

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

Handles ComboBox1.DropDown
With ComboBox1.Items
.Add("Apple")
.Add("Orange")
.Add("Banana")

End With

Answer

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.