Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mutually Exclusive List Boxes

Status
Not open for further replies.

sjf905

Programmer
Sep 20, 2006
13
US
Hello. I have 6 list boxes in a form that I need to be mutually exclusive (ie if an item in listbox1 is selected and the user switches to click on an item in listbox2, I need the listbox1 selection to be unselected, as with every other).

I have been trying some things for a while with no luck. Originally, I tried:

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(listBox2.SelectedIndex > -1){listBox2.ClearSelected(); listBox2.ClearSelected();}
if(listBox3.SelectedIndex > -1){listBox3.ClearSelected(); listBox3.ClearSelected();}
//etc. to listbox6
}

This clears the others fine but also unselects listbox1, looks like listbox1 loses control while the form clears all other boxes, and never gets it back.

I also looked to try the Leave event for a listbox, which would clear the box on exit. This does not fire, which is expected after some research.

Any ideas?
 
Capture the "Click" event. Then validate that something inside the listbox/listview is selected.

If selected, then clear all other list boxes - this will work because you are not capturing "SelectedIndexChanged" but the click instead.

Hope that gets you started.
 
Tried using Click event too when I was getting started. This also does not fire...

"ListBox is derived from ListControl, which is derived from Control. Both of these have Click events listed in the MSDN help, but, as you point out ListBox doesn't. So, while the base classes have this event and therefore the code doesn't complain when trying to wire up the event, the ListBox itself doesn't support that event."

-
 
Do yourself a favour and use a listview instead.

Much more flexible.
 
Assuming you have one of these event handlers for each ListBox, aren't they going to get fired when you clear their selections? Like
Code:
box1 -> select item 1
   -> clear box2 (no change)
   -> clear box3 (was set, SelectionChanged event box3)
   .. reset all other boxes
   .. repeat until all boxes have cleared each other

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top