954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to stop selection from a listbox

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?

Lightning03
Junior Poster in Training
92 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
harishbansal90
Newbie Poster
8 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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();
        }
harishbansal90
Newbie Poster
8 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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.

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

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

mshauny
Junior Poster in Training
62 posts since Jan 2010
Reputation Points: 35
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: