How can I detect that an already slected listbox item has been selected again?

  • Thread starter Thread starter Roberto Rocco
  • Start date Start date
R

Roberto Rocco

Hello,

I know the SelectedIndexChanged event, which is fired when the selected
index has changed.

But I need to handle the situation when an already selected index has been
selected again.

Can anybody give me a hint?

Many thanks in advance,

Roberto Rocco
 
I've put the sort of mechanism you should be looking at... Not sure if the
function names are exactly right but you get the idea.
Thanks
Dan.


-------------------------------------------------------

int PreviousSelectionIndex = -1;

void SelectedIndexChangeEvent (....)
{
if ( PreviousSelectionIndex == myListBox.SelectedIndex )
{
// selected the same one
}
else
{
PreviousSelectionIndex = myListBox.SelectedIndex;

// selected a different item
}
}
 
Hello Dan,

thank you for your reply. Unfortunately your suggestion won't work :-(
The problem is (nomen est omen) that the SelectedIndexChanged event is _NOT_
fired when you click on an already selected item, but only when the selected
index changed!

I need some kind of notification that tells me that a currently selected
index was selected again (the user clicks twice or more times on the same
item.

Any ideas?

Best regards,

Roberto Rocco.
 
The problem is (nomen est omen) that the SelectedIndexChanged event is _NOT_
fired when you click on an already selected item, but only when the selected
index changed!

ah yes, one of the most notorious results of MS's brilliant decision to
sacrifice a true click event in the name of size efficiency

as far as i can tell, the only solution is to derive your own from the base
control class (which does have a click event, fancy that)

or you can just steal someone else's code. i think there is an 'owner-drawn
listbox' on msdn or somewhere that comes with a click event
 
Hello ,

thank you for your reply. One qustion, though: What do you mean by "Click
event"? The event that is raised when I click on the control? If so, how can
I know which item I hav eclicked on? The Click event only returns
coordinates.

Or does the base class really have a "OnSelected" event?

Best regards,

Roberto.
 
Hello,

i've found a solution myself:

// Member variable holding the current index
private int m_CurrentIndex;

private void listBoxEx1_Click(object sender, System.EventArgs e)
{
// Check if the index has been selected again
if (lbEntries.SelectedIndex == m_CurrentIndex)
{
HandleRepeatedSelection();
}
}

Roberto Rocco.
 
Back
Top