Handling ListBox selection changing

  • Thread starter Thread starter KK
  • Start date Start date
K

KK

Hi All...
For my purpose, I need to handle listbox selection changed event.I must
be notified before changing occurs and after.After I can handle using
SelectedIndexChanged event.Is there anyway to know which one is selected
item before this event.?One thing is I can have a variable to hold the index
in my application.

I'm looking exactly like TreeView events(which fires events before
expanding and after expanding the nodes).

Thanks in advance...

Regards
Krishna
 
Krishna,

I think that you will probably have to keep track of the last selected
item in your application. However, you are only going to get one event.
Normally, I would say that you should override the WndProc method on the
ListBox, but there is no windows message either that the listbox dispatches
to indicate that the selection has changed before and after, there is only
one message.

So in that sense, you will have to deal with the single event, and keep
track of the previously selected item(s) yourself.

Hope this helps.
 
Hi,

If you want to do some processing before the SelectedIndexChanged you
should use the MouseDown event, now the real trick is knowing where it was
clicked.
With a listview it's very easy as the ListViewItem provide a bound
property.
In a listbox, well take a look at this piece of code from opennetcf.org 's
OwnerDrawControl:


protected override void OnMouseDown(MouseEventArgs e)
{
//get out if there are no items
if (listItems.Count == 0)
return;

int prevSelection = selectedIndex;

selectedIndex = this.vScroll.Value + (e.Y / this.ItemHeight);

Graphics gxTemp = this.CreateGraphics();

if (prevSelection!=-1)
PaintItem(gxTemp, prevSelection);

PaintItem(gxTemp, selectedIndex);

DrawBorder(gxTemp);

//this.Focus();
base.OnMouseDown( e);
if ( prevSelection != selectedIndex)
OnSelectedIndexChanged( e);
}


Cheers,
 
Back
Top