Listview with Context Menu

  • Thread starter Thread starter Blacksmith
  • Start date Start date
B

Blacksmith

Hi, I want to have a list of items made up of several columns, being able to
select the whole row of one item to bring up a context menu.

The listview in 'details' mode seems like the correct choice, but I have
some questions:

1) How do I ensure the context menu only appears if you tap and hold an
item? i.e. I don't want the context menu to appear if you just tap and hold
some empty part of the list view.

2) How do i detect which item (row) is selected in the listview when the
context menu items click event is fired?

3) Is there any way of making the column headers of the list view fixed, so
that the user cannot resize them?

or should I be using a different control?

Cheers,
BlS
 
Blacksmith said:
Hi, I want to have a list of items made up of several columns, being able to
select the whole row of one item to bring up a context menu.

The listview in 'details' mode seems like the correct choice, but I have
some questions:

1) How do I ensure the context menu only appears if you tap and hold an
item? i.e. I don't want the context menu to appear if you just tap and hold
some empty part of the list view.

2) How do i detect which item (row) is selected in the listview when the
context menu items click event is fired?

3) Is there any way of making the column headers of the list view fixed, so
that the user cannot resize them?

or should I be using a different control?

Cheers,
BlS

Not sure how to achieve (1), but you can use ListView.SelectedIndices[0]
to get the index of the selected item, ie ListViewItem lvi =
myListView.Items[myListView.SelectedIndices[0]];
For (3), what about setting the HeaderStyle to ColumnHeaderStyle.None?
I've done that with one of my ListViews, and just put Labels above the
ListView to simulate the column headers.
Shannon
 
Not sure how to achieve (1), but you can use ListView.SelectedIndices[0]
to get the index of the selected item, ie ListViewItem lvi =
myListView.Items[myListView.SelectedIndices[0]];
For (3), what about setting the HeaderStyle to ColumnHeaderStyle.None?
I've done that with one of my ListViews, and just put Labels above the
ListView to simulate the column headers.
Shannon

Thanks Shannon, I've tried to achieve (1) with the following code.
It seems to work, but the dotted ring that appears when you tap and hold
seems to appear
even when the context menu is set to null (well it does on the Pocket PC
emulator anyway, I haven't tried it on
an actual device yet). It's not a big problem, but slightly annoying.

private void listview_SelectedIndexChanged(object sender, System.EventArgs
e)
{
switch (listview.SelectedIndices.Count)
{
case 0:
// list is empty
listview.ContextMenu = null;
break;

case 1:
// one item is selected
ListViewItem lvItem =
listview.Items[listview.SelectedIndices[0]];

if (lvItem == null)
{
Trace.Assert(false, "Selected Item is Null");
listview.ContextMenu = null;
}
else
{
listview.ContextMenu = contextMenu;
}
break;

default:
Trace.Assert(false, "Should not be able to select more than 1
item!");
listview.ContextMenu = null;
break;
}
}
 
Back
Top