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?

Recommended Answers

All 4 Replies

:(

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();
        }

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.

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

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.