Context Menus

Better ListView allows settings four distinct context menu via properties:



Only ContextMenuStrip is available in Better ListView Express.



These are of type System.Windows.Forms.ContextMenuStrip.

Custom context menus can be displayed manually using MouseDown event and HitTest method to detect location of the cursor.

Sample Source Code

C#

// add column, group and item
this.listView.BeginUpdate();

this.listView.Columns.Add(new BetterListViewColumnHeader("some column", 256));
this.listView.Groups.Add("some group");
this.listView.Items.Add(new BetterListViewItem("some item", this.listView.Groups[0]));

this.listView.ShowGroups = true;

this.listView.EndUpdate();

// set common context menu
ContextMenuStrip contextMenuStripCommon = new ContextMenuStrip();

contextMenuStripCommon.Items.Add("Common Context Menu Item 1");
contextMenuStripCommon.Items.Add("Common Context Menu Item 2");

this.listView.ContextMenuStrip = contextMenuStripCommon;

// set column header context menu
ContextMenuStrip contextMenuStripColumnHeaders = new ContextMenuStrip();

contextMenuStripColumnHeaders.Items.Add("Column Header Context Menu Item 1");
contextMenuStripColumnHeaders.Items.Add("Column Header Context Menu Item 2");

this.listView.ContextMenuStripColumns = contextMenuStripColumnHeaders;

// set item context menu
ContextMenuStrip contextMenuStripItems = new ContextMenuStrip();

contextMenuStripItems.Items.Add("Item Context Menu Item 1");
contextMenuStripItems.Items.Add("Item Context Menu Item 2");

this.listView.ContextMenuStripItems = contextMenuStripItems;

// set group context menu
ContextMenuStrip contextMenuStripGroups = new ContextMenuStrip();

contextMenuStripGroups.Items.Add("Group Context Menu Item 1");
contextMenuStripGroups.Items.Add("Group Context Menu Item 2");

this.listView.ContextMenuStripGroups = contextMenuStripGroups;

Visual Basic

' add column, group and item
ListView.BeginUpdate()

ListView.Columns.Add (New BetterListViewColumnHeader ("some column", 256))
ListView.Groups.Add ("some group")
ListView.Items.Add (New BetterListViewItem ("some item", ListView.Groups (0)))

ListView.ShowGroups = True

ListView.EndUpdate()

' set common context menu
Dim contextMenuStripCommon As New ContextMenuStrip()

contextMenuStripCommon.Items.Add ("Common Context Menu Item 1")
contextMenuStripCommon.Items.Add ("Common Context Menu Item 2")

ListView.ContextMenuStrip = contextMenuStripCommon

' set column header context menu
Dim contextMenuStripColumnHeaders As New ContextMenuStrip()

contextMenuStripColumnHeaders.Items.Add ("Column Header Context Menu Item 1")
contextMenuStripColumnHeaders.Items.Add ("Column Header Context Menu Item 2")

ListView.ContextMenuStripColumns = contextMenuStripColumnHeaders

' set item context menu
Dim contextMenuStripItems As New ContextMenuStrip()

contextMenuStripItems.Items.Add ("Item Context Menu Item 1")
contextMenuStripItems.Items.Add ("Item Context Menu Item 2")

ListView.ContextMenuStripItems = contextMenuStripItems

' set group context menu
Dim contextMenuStripGroups As New ContextMenuStrip()

contextMenuStripGroups.Items.Add ("Group Context Menu Item 1")
contextMenuStripGroups.Items.Add ("Group Context Menu Item 2")

ListView.ContextMenuStripGroups = contextMenuStripGroups