ListView, Removing items.

T

Tim Haughton

I'm having an issue with ListView control. THe list view contains
items that are dynamically added and removed at run time. The problem
comes when I try to remove an item. Whenever an item is removed, the
ListView seems to automagically 'select' the item which sits in the
same spot the recently removed item did.

This only causes a problem when removing the last element in the Items
collection, because there is no element to select, so it throws an
index out of bounds exception.

Anyone know how to stop it autoselecting an item after a removal?

Cheers,

Tim
 
T

Truong Hong Thi

Hi Tim,

I don't think the listview auto selects. It is somewhere in your code
to select. You should update that code to check and if out-of-bound,
select the last item instead.

Thi
 
T

Tim Haugton

That was my original thought, but there really isn't anything in my
code to do this.

A peek at the call stack (included at the end) suggests that the
ListView is initiaing a Right Mouse Button drag operation, which is a
little upsetting.

Cheers,

Tim


system.windows.forms.dll!System.Windows.Forms.ListView.ListViewItemCollection.get_Item(int
displayIndex = 0) + 0xbe bytes

system.windows.forms.dll!System.Windows.Forms.ListView.LvnBeginDrag(System.Windows.Forms.MouseButtons
buttons = Right, System.Windows.Forms.NativeMethods.NMLISTVIEW nmlv =
{System.Windows.Forms.NativeMethods.NMLISTVIEW}) + 0x1f bytes

system.windows.forms.dll!System.Windows.Forms.ListView.WmReflectNotify(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x582 bytes

system.windows.forms.dll!System.Windows.Forms.ListView.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x83 bytes

system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x13 bytes

system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0xda bytes

system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(int
hWnd = 1312878, int msg = 8270, int wparam = 1312878, int lparam =
1239780) + 0x3d bytes
system.windows.forms.dll!System.Windows.Forms.Control.SendMessage(int
msg = 8270, int wparam = 1312878, int lparam = 1239780) + 0x48 bytes

system.windows.forms.dll!System.Windows.Forms.Control.ReflectMessageInternal(int
hWnd = 1312878, System.Windows.Forms.Message m =
{System.Windows.Forms.Message}) + 0x7f bytes

system.windows.forms.dll!System.Windows.Forms.Control.WmNotify(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x2d bytes

system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x664 bytes

system.windows.forms.dll!System.Windows.Forms.ScrollableControl.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x6c bytes

system.windows.forms.dll!System.Windows.Forms.ContainerControl.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x37 bytes

system.windows.forms.dll!System.Windows.Forms.UserControl.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x37 bytes

system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x13 bytes

system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0xda bytes

system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(int
hWnd = 1116172, int msg = 78, int wparam = 1312878, int lparam =
1239780) + 0x3d bytes

system.windows.forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0xd4 bytes

system.windows.forms.dll!System.Windows.Forms.Control.DefWndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x13 bytes

system.windows.forms.dll!System.Windows.Forms.ListView.WmMouseDown(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}, System.Windows.Forms.MouseButtons
button = Right, int clicks = 1) + 0xba bytes

system.windows.forms.dll!System.Windows.Forms.ListView.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x240 bytes

system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x13 bytes

system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0xda bytes

system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(int
hWnd = 1312878, int msg = 516, int wparam = 2, int lparam = 1638433) +
0x3d bytes

system.windows.forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(int
dwComponentID = 1, int reason = -1, int pvLoopData = 0) + 0x349 bytes
system.windows.forms.dll!ThreadContext.RunMessageLoopInner(int reason
= -1, System.Windows.Forms.ApplicationContext context =
{System.Windows.Forms.ApplicationContext}) + 0x1f3 bytes
system.windows.forms.dll!ThreadContext.RunMessageLoop(int reason =
-1, System.Windows.Forms.ApplicationContext context =
{System.Windows.Forms.ApplicationContext}) + 0x50 bytes

system.windows.forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form
mainForm = {EmulatorSuite.GUI.Implementation.EmulatorSuiteView}) + 0x34
bytes
 
T

Truong Hong Thi

The question is: how do you remove the item?
Something to note is that the listview's Items index is 0-based - so be
careful with off-by-one error.
When you remove the last item, I guess the error is because you remove
an out-of-bound item rather than because of auto selection.
Pls check and tell me if that causes the problem.
If it does not help, could you pls post a simple snippet about how you
add and remove items, also include info about .net version.

Thi
 
T

Tim Haugton

The problem was a bit more subtle than that. The user right clicks on
the ListView, which displays a context menu. The user then clicks on
the 'Remove' menu item. The problem was caused because I was handling
the MouseDown event on the ListView to know when and where to display
the context menu, when I should have been handling the MouseUp event.

A very subtle problem.

Cheers,

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top