There is no support for ListBox for this. You can pin something, you can unselect the selected item. Here is a silly example that prevents the selection of even-numbered elements:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { for (int ix = listBox1.SelectedIndices.Count - 1; ix >= 0; ix--) { if (listBox1.SelectedIndices[ix] % 2 != 0) listBox1.SelectedIndices.Remove(listBox1.SelectedIndices[ix]); } }
But the flicker is quite noticeable, and this confuses keyboard navigation. You can get better results using CheckedListBox, you can prevent the user from checking the box for the item:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) { if (e.Index % 2 != 0) e.NewValue = CheckState.Unchecked; }
But now you cannot redefine the drawing to make it obvious to the user that the item is not selectable. There are no big solutions, it’s much easier not to display the items in the box that should not be selected.
source share