public static ContextMenuTag CreateCommonMenuItems( MSWinForms.ContextMenuStrip Menu, MSWinForms.ListView LV, Options options = null) { // Use default options if none were passed if (options == null) options = new Options(); // If no menu passed, use the menu associated with the LV if (Menu == null) Menu = LV.ContextMenuStrip; // // Create the context var Context = new ContextMenuTag() { lv = LV, options = options, menu = Menu }; var DynMenus = new List<MSWinForms.ToolStripItem>(); // // Copy/Select All if (options.MFlags.HasFlag(MenuFlags.CopyAndSelect)) { // Copy Context.CopyItemMenu = new MSWinForms.ToolStripMenuItem() { ShortcutKeys = (MSWinForms.Keys.Control | MSWinForms.Keys.C), Text = "Copy" }; Context.CopyItemMenu.Click += new EventHandler(menuCommonLVCopyItem_Click); // Select All Context.SelectAllMenu = new MSWinForms.ToolStripMenuItem() { ShortcutKeys = (MSWinForms.Keys.Control | MSWinForms.Keys.A), Text = "Select all" }; Context.SelectAllMenu.Click += new EventHandler(menuCommonLVSelectAll_Click); // DeSelect All Context.DeSelectAllMenu = new MSWinForms.ToolStripMenuItem() { ShortcutKeys = (MSWinForms.Keys.Control | MSWinForms.Keys.D), Text = "De-Select all" }; Context.DeSelectAllMenu.Click += new EventHandler(menuCommonLVDeSelectAll_Click); DynMenus.AddRange(new MSWinForms.ToolStripItem[] { Context.CopyItemMenu, Context.SelectAllMenu, Context.DeSelectAllMenu, new MSWinForms.ToolStripSeparator(), }); } // // Find if (options.MFlags.HasFlag(MenuFlags.Find)) { // FindFirst Context.FindFirstMenu = new MSWinForms.ToolStripMenuItem() { ShortcutKeys = (MSWinForms.Keys.Control | MSWinForms.Keys.F), Text = "Find", }; Context.FindFirstMenu.Click += new EventHandler(menuCommonLVFindFirst_Click); // FindNext Context.FindNextMenu = new MSWinForms.ToolStripMenuItem() { ShortcutKeys = MSWinForms.Keys.F3, Text = "Find Next", }; Context.FindNextMenu.Click += new EventHandler(menuCommonLVFindNext_Click); DynMenus.AddRange(new MSWinForms.ToolStripItem[] { Context.FindFirstMenu, Context.FindNextMenu, new MSWinForms.ToolStripSeparator(), }); } // // Delete if (options.MFlags.HasFlag(MenuFlags.Delete)) { // Delete Context.DeleteMenu = new MSWinForms.ToolStripMenuItem() { ShortcutKeys = MSWinForms.Keys.Delete, Text = "Delete" }; Context.DeleteMenu.Click += new EventHandler(menuCommonLVDeleteItem_Click); DynMenus.AddRange(new MSWinForms.ToolStripItem[] { Context.DeleteMenu, new MSWinForms.ToolStripSeparator() }); } // // Export if (options.MFlags.HasFlag(MenuFlags.Export)) { var ExportToTextFile = new MSWinForms.ToolStripMenuItem() { ShortcutKeys = (MSWinForms.Keys.Control | MSWinForms.Keys.S), Text = "Export to text file" }; ExportToTextFile.Click += new EventHandler(menuCommonLVExportToTextFile_Click); DynMenus.AddRange(new MSWinForms.ToolStripItem[] { ExportToTextFile, new MSWinForms.ToolStripSeparator() }); } // // Add all the dynamic menu items now if (DynMenus.Count > 0) { if (DynMenus[DynMenus.Count - 1] is MSWinForms.ToolStripSeparator) DynMenus.RemoveAt(DynMenus.Count - 1); foreach (var m in DynMenus) Menu.Items.Add(m); } // // Dynamic menu to be created based on column item click? if (options.OnGenColItemMenuItem != null && options.OnColItemMenuClick != null) { Menu.Opening += new CancelEventHandler(menuCommonLV_Opening); Menu.Closed += new MSWinForms.ToolStripDropDownClosedEventHandler(menuCommonLV_Closed); } // // Install column sorter if (options.WantColSorting) LV.ColumnClick += new MSWinForms.ColumnClickEventHandler(lvCommon_ColumnClick); // // Associate the context with the tags of the listview and the menu Menu.Tag = Context; LV.Tag = Context; // Associate the menu with the LV LV.ContextMenuStrip = Menu; return Context; }
private static void menuCommonLV_Opening( object sender, CancelEventArgs e) { // Get context var lvCtx = GetCommonLVContext(sender); if (lvCtx == null) return; // Bail out if no items selected var lv = lvCtx.lv; if (lv.SelectedItems.Count == 0) return; // Get the first selected item var lvi = lv.SelectedItems[0]; // // Determine the column index where the click occured Point mousePosition = lv.PointToClient(MSWinForms.Control.MousePosition); MSWinForms.ListViewHitTestInfo hit = lv.HitTest(mousePosition); if (hit.SubItem == null) return; int ColIdx = hit.Item.SubItems.IndexOf(hit.SubItem); // // Call user callback to get some contextual info UserMenuItem[] UserMenus; bool bProceed = lvCtx.options.OnGenColItemMenuItem( ColIdx, lvi.SubItems[ColIdx].Text, out UserMenus); if (!bProceed) { e.Cancel = true; return; } // Generate a dynamic menu item lvCtx.GenColItemMenu.Add(new MSWinForms.ToolStripSeparator()); foreach (var m in UserMenus) { var menu = new MSWinForms.ToolStripMenuItem() { Text = m.Text, Tag = m.Tag, }; menu.Click += lvCtx.options.OnColItemMenuClick; lvCtx.GenColItemMenu.Add(menu); } // Insert the user menus foreach (var mi in lvCtx.GenColItemMenu) lvCtx.menu.Items.Add(mi); }