public static void ShowIconMenu <T>(Point position, IEnumerable <Tuple <T, Bitmap> > items, Action <T> pickHandler) { List <Tuple <T, Bitmap> > itemList = items?.OrderBy(item => item.Item1.ToString()).ToList(); if (itemList == null || !itemList.Any()) { return; } ContextMenuStrip contextMenu = new ContextMenuStrip(); ToolStripDropDownClosedEventHandler contextMenuClosedEventHandler = null; ToolStripItemClickedEventHandler menuItemClickedEventHandler = null; contextMenuClosedEventHandler = (sender, e) => { contextMenu.Closed -= contextMenuClosedEventHandler; contextMenu.ItemClicked -= menuItemClickedEventHandler; }; menuItemClickedEventHandler = (sender, e) => { contextMenu.Closed -= contextMenuClosedEventHandler; contextMenu.ItemClicked -= menuItemClickedEventHandler; contextMenu.Close(); pickHandler((T)e.ClickedItem.Tag); }; contextMenu.Closed += contextMenuClosedEventHandler; contextMenu.ItemClicked += menuItemClickedEventHandler; itemList.ForEach(item => contextMenu.Items.Add(new ToolStripMenuItem(item.Item1.ToString()) { Tag = item.Item1, Image = item.Item2 })); contextMenu.Show(position); }
protected override void OnIntelliPromptSmartTagClicked(EventArgs e) { ToolStripDropDownClosedEventHandler handler = null; SmartTag tag = base.get_IntelliPrompt().get_SmartTag().get_ActiveSmartTag(); if ((tag != null) && (tag.get_Tag() is NamespaceCompletionData)) { string str; if (base.get_IntelliPrompt().get_MemberList().get_Visible()) { base.get_IntelliPrompt().get_MemberList().Abort(); } base.get_IntelliPrompt().get_SmartTag().set_AutoHideTimeout(0xea60); NamespaceCompletionData data = (NamespaceCompletionData)tag.get_Tag(); ContextMenuStrip strip = new ContextMenuStrip(); foreach (NamespaceSuggestion suggestion in data.Suggestions) { NamespaceSuggestion localItem = suggestion; str = (localItem.ExtraReference == null) ? "" : (" (in " + Path.GetFileName(localItem.ExtraReference) + ")"); strip.Items.Add("using " + localItem.Namespace + str, null, delegate(object sender, EventArgs e) { if (localItem.CaseFixedIdentifier != null) { this.get_Document().ReplaceText(0, new TextRange(data.Offset, data.Offset + data.Length), localItem.CaseFixedIdentifier); } MainForm.Instance.AddNamespaceToQuery(localItem.Namespace); if (localItem.ExtraReference != null) { MainForm.Instance.AddReferenceToQuery(localItem.ExtraReference, true); } this.get_IntelliPrompt().get_SmartTag().Hide(); this.get_IntelliPrompt().get_SmartTag().Clear(); this.AddedNamespace(); }); } strip.Items.Add("-"); foreach (NamespaceSuggestion suggestion in data.Suggestions) { NamespaceSuggestion localItem = suggestion; str = (localItem.ExtraReference == null) ? "" : (" (in " + Path.GetFileName(localItem.ExtraReference) + ")"); strip.Items.Add(localItem.Namespace + "." + (localItem.CaseFixedIdentifier ?? data.Identifier) + str, null, delegate(object sender, EventArgs e) { this.get_Document().ReplaceText(0, new TextRange(data.Offset, data.Offset + data.Length), localItem.Namespace + "." + (localItem.CaseFixedIdentifier ?? data.Identifier)); if (localItem.ExtraReference != null) { MainForm.Instance.AddReferenceToQuery(localItem.ExtraReference, true); } this.AddedNamespace(); }); } if (handler == null) { handler = (sender, e) => base.get_IntelliPrompt().get_SmartTag().set_AutoHideTimeout(0x2710); } strip.Closed += handler; strip.Items[0].Select(); Rectangle rectangle = base.get_IntelliPrompt().get_SmartTag().get_DesktopBounds(); strip.Show(rectangle.X, rectangle.Y + rectangle.Height); } base.OnIntelliPromptSmartTagClicked(e); }
protected virtual void OnClosed(ToolStripDropDownClosedEventArgs e) { ToolStripDropDownClosedEventHandler eh = (ToolStripDropDownClosedEventHandler)(Events [ClosedEvent]); if (eh != null) { eh(this, e); } }
/// <summary> /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed. /// <example> /// toolstripdropdownclosedeventhandler.BeginInvoke(sender, e, callback); /// </example> /// </summary> public static IAsyncResult BeginInvoke(this ToolStripDropDownClosedEventHandler toolstripdropdownclosedeventhandler, Object sender, ToolStripDropDownClosedEventArgs e, AsyncCallback callback) { if (toolstripdropdownclosedeventhandler == null) { throw new ArgumentNullException("toolstripdropdownclosedeventhandler"); } return(toolstripdropdownclosedeventhandler.BeginInvoke(sender, e, callback, null)); }
protected override void OnClick(EventArgs e) { base.OnClick(e); if(this.DropDown != null) { if(!this.DroppedDown) { this.DroppedDown = true; this.closedEventHandler = new ToolStripDropDownClosedEventHandler(DropDown_Closed); this.DropDown.Closed += this.closedEventHandler; this.DropDown.Show(this, new System.Drawing.Point(0, this.Height), ToolStripDropDownDirection.BelowLeft); if(this.DropDownShown != null) this.DropDownShown(this, EventArgs.Empty); } } }
public static void ShowMenu <T1, T2>(Point position, IEnumerable <T1> rootItems, IEnumerable <T2> items, Action <T1, T2> pickHandler) { IEnumerable <T1> rootItemList = rootItems as IList <T1> ?? rootItems?.ToList(); if (rootItemList == null || !rootItemList.Any()) { return; } List <T2> itemList = items?.OrderBy(item => item.ToString()).ToList(); if (itemList == null || !itemList.Any()) { return; } ContextMenuStrip contextMenu = new ContextMenuStrip(); foreach (T1 rootItem in rootItemList) { ToolStripMenuItem rootMenuItem = new ToolStripMenuItem(rootItem.ToString()) { Tag = rootItem }; contextMenu.Items.Add(rootMenuItem); itemList.ForEach(item => rootMenuItem.DropDownItems.Add(new ToolStripMenuItem(item.ToString()) { Tag = new Tuple <T1, T2>(rootItem, item) })); } ToolStripDropDownClosedEventHandler contextMenuClosedEventHandler = null; ToolStripItemClickedEventHandler menuItemClickedEventHandler = null; contextMenuClosedEventHandler = (sender, e) => { contextMenu.Closed -= contextMenuClosedEventHandler; contextMenu.Items.Cast <ToolStripMenuItem>().ToList().ForEach(x => x.DropDownItemClicked -= menuItemClickedEventHandler); }; menuItemClickedEventHandler = (sender, e) => { contextMenu.Closed -= contextMenuClosedEventHandler; contextMenu.Items.Cast <ToolStripMenuItem>().ToList().ForEach(x => x.DropDownItemClicked -= menuItemClickedEventHandler); contextMenu.Close(); Tuple <T1, T2> tag = (Tuple <T1, T2>)e.ClickedItem.Tag; pickHandler(tag.Item1, tag.Item2); }; contextMenu.Closed += contextMenuClosedEventHandler; contextMenu.Items.Cast <ToolStripMenuItem>().ToList().ForEach(x => x.DropDownItemClicked += menuItemClickedEventHandler); contextMenu.Show(position); }
public static void ShowMultiSelectMenu <T>(Point position, IEnumerable <Tuple <T, bool> > items, Action <List <T> > pickHandler) { List <Tuple <T, bool> > itemList = items?.OrderBy(item => item.Item1.ToString()).ToList(); if (itemList == null || !itemList.Any()) { return; } bool anyItemClicked = false; ContextMenuStrip contextMenu = new ContextMenuStrip(); ToolStripDropDownClosedEventHandler contextMenuClosedEventHandler = null; ToolStripDropDownClosingEventHandler contextMenuClosingEventHandler = null; ToolStripItemClickedEventHandler menuItemClickedEventHandler = null; contextMenuClosedEventHandler = (sender, e) => { contextMenu.Closed -= contextMenuClosedEventHandler; contextMenu.Closing -= contextMenuClosingEventHandler; contextMenu.ItemClicked -= menuItemClickedEventHandler; }; contextMenuClosingEventHandler = (sender, e) => { if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked) { e.Cancel = true; ((ToolStripDropDownMenu)sender).Invalidate(); } else if (anyItemClicked) { List <T> selectedItems = ((ToolStripDropDownMenu)sender).Items.Cast <ToolStripMenuItem>().Where(x => x.Checked).Select(x => (T)x.Tag).ToList(); pickHandler(selectedItems); } }; menuItemClickedEventHandler = (sender, e) => { ToolStripMenuItem clickedItem = (ToolStripMenuItem)e.ClickedItem; clickedItem.Checked = !clickedItem.Checked; anyItemClicked = true; }; contextMenu.Closed += contextMenuClosedEventHandler; contextMenu.Closing += contextMenuClosingEventHandler; contextMenu.ItemClicked += menuItemClickedEventHandler; itemList.ForEach(item => contextMenu.Items.Add(new ToolStripMenuItem(item.Item1.ToString()) { Tag = item.Item1, Checked = item.Item2 })); contextMenu.Show(position); }
private Task <ShowContextMenuResponse> ShowMenu(ShowContextMenuParameters parameters) { TaskCompletionSource <ShowContextMenuResponse> tcs = new TaskCompletionSource <ShowContextMenuResponse>(); SpellCheckMenu spellCheckMenu = parameters.SpellCheckMenu; if (spellCheckMenu != null) { BeginInvoke(new Action(() => { ContextMenuStrip popupMenu = new ContextMenuStrip(); IEnumerable <string> suggestions = spellCheckMenu.DictionarySuggestions; if (suggestions != null) { // Add menu items with suggestions. foreach (string suggestion in suggestions) { popupMenu.Items.Add(BuildMenuItem(suggestion, true, delegate { browser.ReplaceMisspelledWord(suggestion); tcs.TrySetResult(ShowContextMenuResponse.Close()); })); } } // Add "Add to Dictionary" menu item. string addToDictionary = spellCheckMenu.AddToDictionaryMenuItemText ?? "Add to Dictionary"; popupMenu.Items.Add(BuildMenuItem(addToDictionary, true, delegate { if (!string.IsNullOrWhiteSpace(spellCheckMenu.MisspelledWord)) { engine.Profiles.Default.SpellChecker?.CustomDictionary ?.Add(spellCheckMenu.MisspelledWord); } tcs.TrySetResult(ShowContextMenuResponse.Close()); })); // Close context menu when the browser requests focus back. EventHandler <FocusRequestedEventArgs> onFocusRequested = null; onFocusRequested = (sender, args) => { BeginInvoke((Action)(() => popupMenu.Close())); parameters.Browser.FocusRequested -= onFocusRequested; }; parameters.Browser.FocusRequested += onFocusRequested; // Handle the menu closed event. ToolStripDropDownClosedEventHandler menuOnClosed = null; menuOnClosed = (sender, args) => { bool itemNotClicked = args.CloseReason != ToolStripDropDownCloseReason.ItemClicked; if (itemNotClicked) { tcs.TrySetResult(ShowContextMenuResponse.Close()); } popupMenu.Closed -= menuOnClosed; }; popupMenu.Closed += menuOnClosed; // Show the context menu. Point location = new Point(parameters.Location.X, parameters.Location.Y); popupMenu.Show(this, location); tcs.TrySetResult(ShowContextMenuResponse.Close()); })); } else { tcs.TrySetResult(ShowContextMenuResponse.Close()); } return(tcs.Task); }
private Task <ShowContextMenuResponse> ShowMenu(ShowContextMenuParameters parameters) { TaskCompletionSource <ShowContextMenuResponse> tcs = new TaskCompletionSource <ShowContextMenuResponse>(); SpellCheckMenu spellCheckMenu = parameters.SpellCheckMenu; if (spellCheckMenu != null) { BeginInvoke(new Action(() => { ContextMenuStrip popupMenu = new ContextMenuStrip(); if (!string.IsNullOrEmpty(parameters.LinkText)) { ToolStripItem buildMenuItem = BuildMenuItem("Show the URL link", true, (sender, args) => { string linkURL = parameters.LinkUrl; Console.WriteLine($"linkURL = {linkURL}"); MessageBox.Show(linkURL, "URL"); tcs.TrySetResult(ShowContextMenuResponse.Close()); }); popupMenu.Items.Add(buildMenuItem); } ToolStripItem reloadMenuItem = BuildMenuItem("Reload", true, (sender, args) => { Console.WriteLine("Reload current web page"); browser.Navigation.Reload(); tcs.TrySetResult(ShowContextMenuResponse.Close()); }); popupMenu.Items.Add(reloadMenuItem); // Close context menu when the browser requests focus back. EventHandler <FocusRequestedEventArgs> onFocusRequested = null; onFocusRequested = (sender, args) => { BeginInvoke((Action)(() => popupMenu.Close())); parameters.Browser.FocusRequested -= onFocusRequested; }; parameters.Browser.FocusRequested += onFocusRequested; // Handle the menu closed event. ToolStripDropDownClosedEventHandler menuOnClosed = null; menuOnClosed = (sender, args) => { bool itemNotClicked = args.CloseReason != ToolStripDropDownCloseReason.ItemClicked; if (itemNotClicked) { tcs.TrySetResult(ShowContextMenuResponse.Close()); } popupMenu.Closed -= menuOnClosed; }; popupMenu.Closed += menuOnClosed; // Show the context menu. Point location = new Point(parameters.Location.X, parameters.Location.Y); popupMenu.Show(this, location); tcs.TrySetResult(ShowContextMenuResponse.Close()); })); } else { tcs.TrySetResult(ShowContextMenuResponse.Close()); } return(tcs.Task); }