"Jure Bogataj" <jure.bogataj@mikrocop.comwrote in message
news:%23BruUy$DIHA.2268@TK2MSFTNGP02.phx.gbl...
Quote:
Hi all!
>
I have a problem (performance issue) with listview. I have implemented an
ItemSelectionChange on my listview and put some code in it (I build some
toolbar based on selection and update info in statusbar). When selecting
one item (clicking on listview) it works fast, without noticing. However
if selecting multiple items with SHIFT (approx. 500 items) or selecting
with mouse, for each item selected through all listview an event
(ItemSelectionChange) gets called. And this is what slows down my app (the
code running 500 times instead of one time - OnSelectionEnd or sth.). Is
it possible to have an event or simulate it only when selection is
finished (e.g. when all 500 items are selected) not for each and every
one?
>
Thanks in advance!
>
Best regards,
Jure
Hi Jure,
It is possible to create a new event like that, and handle it, but it would
be extremely difficult. You would have to track down all of the windows
messages that could potentially cause a selection change, and manage them.
There's a lot of behavior managing the selection of items in the listview.
For example, you can move the focus rectangle while holding the 'Ctrl' key,
and then select individual items with the spacebar.
A better way perhaps for you would be to put a minimal delay in your
ItemSelectionChange Handler. Say -- 50ms. Use a timer, Once the selection
changes, restart the timer. If the selection changed more than once within
the delay period, then the original is ignored, but after the delay has
expired, the logic is executed.
Like this:
public class SelectionEndListView : ListView
{
private System.Windows.Forms.Timer m_timer;
private const int SELECTION_DELAY = 50;
public SelectionEndListView()
{
m_timer = new Timer();
m_timer.Interval = SELECTION_DELAY;
m_timer.Tick += new EventHandler(m_timer_Tick);
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
// restart delay timer
m_timer.Stop();
m_timer.Start();
}
private void m_timer_Tick(object sender, EventArgs e)
{
m_timer.Stop();
// Perform selection end logic.
Console.WriteLine("Selection Has Ended");
}
}
-- Matt