Help: Removing item from ListView

S

schaf

Hi All!
I have a question about removing my selected item from the listview
control.

My listview lstView contains listviewitems which contains another
object in the tag property.
After selecting one of the listviewitems and pressing the delete item
in the context menu the following code will be executed:

//Log the start of the deletion

for (int i = lstView.SelectedItems.Count - 1; i >= 0; i--) {
ListViewItem item = lstView.SelectedItems;
CListViewObject listViewObject = item.Tag as
CListViewObject;
IPerson person = listViewObject.person;
//Event informing the main class that the corrsponding
person can be deleted.
OnItemDeleted(person);
lstView.Items.Remove(item);
}

If I do not remove the item no exception will be fired. Otherwise a
the following exception occurs:

System.ArgumentOutOfRangeException: Specified argument was out of the
range of valid values.
Parameter name: '0' is not a valid value for 'displayIndex'.
at System.Windows.Forms.ListViewItemCollection.get_Item(Int32
displayIndex)
at System.Windows.Forms.ListView.LvnBeginDrag(MouseButtons buttons,
NMLISTVIEW nmlv)
at System.Windows.Forms.ListView.WmReflectNotify(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)

I'm using .NET 1.1 and due to no supported ToolTipText on
listViewItems i programmed a tooltiptext, which appears while the
mouse move event on the ListView. Could that be the problem or is the
event a problem ?

if (e.Button == MouseButtons.Right) {
ListViewItem listViewItem = this.lstView.GetItemAt(e.X, e.Y);
if (listViewItem != null) {
listViewItem.Selected = true;
CListViewObject listViewObject = item.Tag as
CListViewObject;
if (listViewObject != null) {
if(listViewObject.IsCustomer() == false) {
cntMnuItem.Show(lstView, new Point(e.X, e.Y));
}
}
}
}


Thanks for each hint.
Regards Marcel
 
S

schaf

I found the problem myself.
I used the MouseDown event for calling the conext menu. By using the
mouse up event everything works

regards
 
C

Claes Bergefall

Consider using WM_CONTEXTMENU to show your context menu instead of mouse
events. That's the intended message. There are certain situations when
showing a context menu in response to mouse down/up causes the application
to break (mainly involving drag and drop). Furthermore, the mouse events are
obviously not fired when using the keyboard to show a context menu
(WM_CONTEXTMENU is).

Unfortunately it requires inheriting the listview and overriding its
WndProc. It's simple to do though:

private const int WM_CONTEXTMENU = 123;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CONTEXTMENU)
{
//This is the position of the cursor where the user clicked to
invoke the menu. If the keyboard was used it will be (-1, -1)
Point pos = new Point(m.LParam.ToInt32());

//Do your thing here

m.Result = IntPtr.Zero;
return;
}
base.WndProc(ref m);
}

(I hope this compiles. It's a free hand translation of VB.NET code)

/claes
 

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