I have a problem using Infragistics UltraTree control.
I need a way to add a specific context menu to an UltraTreeNode.
I saw that there is a contextMenu property for the UltraTree, but I need a unique one per each node.
what is the best way for doing this in C#?
I've recently had to do this with a regular TreeView. Since infragistics.UltraTreeNode has a context menu just like the regular TreeNode, I'm guessing the technique will be the same.
Here's how I did it:
Step 1. Assign the context menu of interest to each node that should recieve it. This should be done while populating tree nodes.
Step 2. Intercept the Opening event on the context menu(s)
Step 3. Rebuild the contents of the menu in the "contextMenu_Opening" event handler. Only do this for a context menu object that will be shared among different node types. You can also add all items you want to the menu, up front on the treeview or containing controls construction, then hide or show them through the Visible property. This is really a matter of personal preference at this point.
Related
Inheriting from ListView control for WinForms and I would like to display a context menu when a item is selected and a totally different one when there is none.
This approach seems straight forward and I saw lots of examples that basically listen to the right click event and upon that decide which context to show and force it to show at cursor location.
What I am looking for if there is a more efficient / native method of doing it rather than listening to the right click event, for starters it will not work if you try to open the context from the keyboard (either shift+F10 or with context key). Plus, if you are using keyboard to navigate the listview and your cursor is elsewhere then it looks horrible. On top of that the menu should be shown on mouseUp and not mouseDown which make it a bit more complicated.
I was looking for an event of the base control class that would handle the "onContextInvoked" or similar, even onContextOpening but at the control level not the context menu level.
Anyone has a better feedback/solution/approach to this?, or I just have to deal with mouse/key events and force to show a context which is not actually bounded to the control itself?
One idea I had is to always use one context menu as normal and then on the opening event of that one check if items are selected and if not try to cancel that context and open the other one on that very same position, but I haven't tried it yet.
I have a menu in a WPF application that I would like to update each time the user open it because the MenuItems are from a DataBase that can change at any time.
Is there a way to rebuild theses MenuItems each time by subscribing to an event or something?
Regards
Consider binding the menu items to some sort of collection in your view model. The collection would contain the menu items, and the UI would bind to it, through a data template.
The blog post at http://blogs.msdn.com/b/llobo/archive/2007/10/23/binding-menus-using-heirarchicaldatatemplates.aspx should help describe this technique. Another post at http://zamjad.wordpress.com/2009/12/18/using-hierarchical-data-template-with-menu-item/ also contains good information.
I have a simple winform app with textbox.
I didnt assigned any context menu to text field and "it uses" standard one. I desire to add some new items to textbox's standard context menu. But I cant obtain it for modification, if be more clearly I didnt know how to obtain HMENU native object.
ContextMenu property of my textbox equals null so I suppose that menu appers within textbox defaul windProc routing. It there a way to get the default context menu or get a copy of it?
Thanks in advance!
Depending on any plug-ins you're using, it may be easier, and more understandable, to create a custom control that derives from TextBox and defines a "default" contextual menu. Then, change all TextBoxes in your solution to be your custom control instead. Failing that, if you have a common window ancestor, set up some initialization code that assigns a given ContextMenu to all controls in the form's hierarchy of type TextBox. Instead of plugging in to low-level Windows hooks, you're using what the framework gives you to accomplish the same end, and your posterity doesn't have to know how Windows handles contextual menus at a message-passing level in order to alter this behavior.
In my application I have dropdown list with several items. I'd like to show a context menu when the user clicks the right mouse button on a dropdown item. Is this possible? And if it is possible, how?
It is possible but not easy. The ComboBox dropdown is a native ListBox that is created on-the-fly. To get the handle of that list box, you have to send the CB_GETCOMBOBOXINFO message in the DropDown event. Check my answer in this thread to find out how to do this.
The iceberg that is likely to sink that Titanic is that the dropdown automatically closes as soon as it loses focus. Which will happen as soon as you display the context menu. Nothing you can do about that.
Consider a different approach, you could use an actual ListBox that you make visible when the user clicks a glyph that looks like an arrow next a TextBox.
Not possible easily. No hover or right-click event messages are being sent when the combobox is expanded.
You can see this is the case when using Spy++.
I think the easiest would be to change to a listbox if your scenario allows you to do that.
As Wim said in his post, there's not a direct way to do this because the messages you want aren't fired.
As a comprise, you could try setting DropdownStyle=Simple; on the Combo and
shrink the scroll region to show a single line:
http://img34.imageshack.us/img34/1695/49557147.jpg
If you assign the Combo a context menu, it will open when the scroll region is right-clicked. You'd probably have to figure out what item was right-clicked. But as other have said, this doesn't sound like a standard Windows way or an intuitive use.
Or how about a modal dialog that you could bring up from the Combo's context menu? On the dialog, you could have a list that the user could select from and a Delete button to delete the selected item(s).
Since MenuItem doesn't have a ContextMenu property it isn't as easy as it is with many other controls. You'll probably need to capture the right-click event and then position and show the context menu manually.
Two caveats though:
The combobox list will close when the
list loses focus, so it may close
when the context menu is shown
leaving the user unclear as to what
they clicked on (not 100% sure on
this, since I haven't written the
code to test.)
More importantly though I would argue
that this is a poor UI choice, I
can't think of any real world
applications I've used that have
context menus on menu items, so it
wouldn't be very discoverable for the
end user. Plus context menus should
be just for quick access, they
shouldn't be the only way to access
functionality. Are you going to be
able to expose these functions
through other means as well as the right-click menu?
You could do it manually, by capturing the event on the form, but consider making a nested menu instead. If your combobox items have menus of their own, combobox probably isn't the right choice.
For those asking "Why?" or saying they've never seen a combobox with a contextmenu; Look at any web browser's favorites dropdown. You can right click and delete, edit, or go to the entry in current tab, new tab, or new window.
I also could not get an actual contextmenustrip to show, so I made a menu with a small borderless form and call with:
Private Sub FavoritesBar_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles FavoritesBar.SelectedIndexChanged
FavIndex = FavoritesBar.SelectedIndex 'FavIndex is declared as a public string.
Dim Loc As Point = New Point(MousePosition)
FavMenu.Location = Loc
FavMenu.ShowDialog()
End Sub
With a homemade contextmenu, be sure to add "me.close" at the end of each sub as well as the me.mouseleave event.
I am creating a menu that will have its menu items injected into it from different modules (I am using the Composite Application Library: Unity, Prism).
Example:
File
- Open
- New
- A (injected from ModuleA)
- B (injected from ModuleB)
- C (injected from ModuleC)
- Exit
Because I don't know which items will be injected, I need to somehow sort my Menu Items. I am trying to do this now by creating a RankedMenuItem that inherits from MenuItem. RankedMenuItem has one additional property, Rank, which is an integer.
I set the "File -> Exit" menu item to be rank 1000, and "File -> Open" to be rank 0, so that Exit will always show up at the bottom of the File menu, and Open will always show up at the top.
The trouble I'm having is that I can't figure out the best way to sort the menu hierarchy. I can sort the top level easily using a CollectionView, but I can't figure out how to apply it to the subitems.
Any advice would be much appreciated.
By default the MenuItem.ItemsSource can contain any collection-based set. This can be a sorted list, for example, or an collectionviewsource. If the items in the collection are domain-specific (e.g. not MenuItem objects) then your task may be less-than-trivial. Based on your description it sounds as though your modules register a simple MenuItem object directly into the Items of the "New" MenuItem, thus making it impossible to perform a sort. If that's the case, my suggestion is to have something like ObversableCollection which items are added/removed via, and use that via a CollectionViewSource attached to the MenuItems property of the "New" menu-item. This should allow you to sort menu items added dynamically by other modules (though, how you implement the sort is up to you, e.g. requiring a RankedMenuItem from the modules, or you could simply sort alphabetically with some product-specific "weighting" to ensure some menu items are always sorted above/below other "known" menu items.)
Hope that helps.