Hello! I wonder if someone can help me. I have a combobox named Requirements and The values of it come from sql. I want to ask how can I detect if I already selected one item to avoid selecting it again the next time I dropdown the combo box. Thank you very much and God Bless. :D

Dani AI

Generated

The replies already on this thread point in the right direction. suggested remembering a previously selected value, implemented a working list-of-indexes approach, and offered a bitmask variant. Those will work for simple cases, but they break or become fragile when the ComboBox is data-bound or reloaded from SQL (indices change) and when multiple distinct selections must be remembered. A more robust pattern is to track the underlying key/value (ValueMember) instead of the SelectedIndex, use a fast lookup collection, and protect against event recursion when programmatically changing the selection.

Example pattern (VB.NET, WinForms): bind the ComboBox with DisplayMember and ValueMember, keep a HashSet of selected IDs, and in SelectedIndexChanged check SelectedValue, revert to the previous index if the ID is already in the set, and use a small flag to ignore the event while restoring selection.

Private selectedIds As New HashSet(Of Integer)
Private prevIndex As Integer = -1
Private ignoreSelection As Boolean = False

' After binding: ComboBox1.DisplayMember = "Name" : ComboBox1.ValueMember = "ID"

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    If ignoreSelection Then Return
    Dim val = ComboBox1.SelectedValue
    If val Is Nothing OrElse val Is DBNull.Value Then
        prevIndex = ComboBox1.SelectedIndex
        Return
    End If
    Dim id As Integer = Convert.ToInt32(val)
    If selectedIds.Contains(id) Then
        ignoreSelection = True
        MessageBox.Show("This item was already selected.")
        ComboBox1.SelectedIndex = prevIndex
        ignoreSelection = False
    Else
        selectedIds.Add(id)
        prevIndex = ComboBox1.SelectedIndex
    End If
End Sub

Notes and troubleshooting: prefer tracking the database primary key (ValueMember) so selections remain correct after rebinds; use HashSet for O(1) lookups (better than linear List for many items); to remove previously chosen items from the dropdown, filter the data source and rebind rather than trying to “disable” items (ComboBox doesn’t support per-item disabling by default); guard initialization so SelectedIndexChanged logic does not run while the control is first bound.

Recommended Answers

All 5 Replies

Maybe not the best way to do it, but...

You can set a variable equal to the selected value (would have to be global inside the class). Then in your .SelectedIndexChanged function, you can put your code inside an If statement that tests to see if the newly selected value is the same as before.

There might be some better ways to do this, but that would work... i think!

Good Luck!

Hi! I got your idea but I think that would only work after 1 selectedindex. Because since, I have more items and what I need is to check if item/s have already been selected before. Thank you and God Bless. :)

See if this helps.

Public Class Form1
    Private myCoolList As New List(Of Integer) '// List to store your SelectedIndexes.

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Not myCoolList.Contains(ComboBox1.SelectedIndex) Then '// check if not already added.
            myCoolList.Add(ComboBox1.SelectedIndex) '// add to list.
        Else
            MsgBox("This item has been previously viewed.") '// alert if already viewed.
        End If
    End Sub
End Class
myCoolList.Clear() '// Clear the List if needed.

Thanks! It works. God Bless both of you. :)

Dim a, selected, prev As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        a = 1
        selected = 0
    End Sub

 a = a << ComboBox1.SelectedIndex
        If (Convert.ToBoolean(selected And a)) Then
            ComboBox1.SelectedIndex = prev
        Else
            selected = selected Or a
            a = 1
            prev = ComboBox1.SelectedIndex
        End If

Originally posted by codeorder

What happen in this code it uses extra amount of memory and when we selected the item which is previously once selected just give a message but that selected with message.

Check If my code is work for you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.