0

how do I disable the selection of an item in a listbox?

For example I already selected item1 from the listbox, I cannot select that same item1 again. How do i do that?

4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by mshauny
0

I dont know how to disabled particular items in List box. Alternatively you can alert user to not select the already selected item. :P

string item = "";
        private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ListBox1.SelectedItem.ToString() == item)
            {
                MessageBox.Show("Already selected!");
            }
            item = ListBox1.SelectedItem.ToString();
        }

Edited by harishbansal90: n/a

0

Not quite sure what you mean by ".. already selected... cannot select that item again".
Two ideas that might help you achieve what you want.
1. Remove the item once selected - (perhaps moving it to another listbox).
2. Set SelectionMode to MultiSimple - that way once it is selected it stays selected until clicked again.

0

You have to be more specific as to what are you hoping to archive.
1. as Nick has said, you have an option of removing it from the list (if you don't want it to be selected twice), but what difference will it make if it's selected twice as the index will be the same, unless you have multiple items which are the same.
2. immediately after a selection has been made, disable the listbox until some condition has been met. lstbox.Enable=false; which you'll still have access to it programmatically

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.