What are my options for opening up a context menu for your grid control.
I found that I could either add a paticular context menu to the grid control or open up a context menu on the mouse down event
The problem with adding the context menu to the control is that I don't know how to then offer difrent menu options depending on which cells was clicked.
I can do this with the mouse down event route but I then have a strange quirk that is annoying me where if you miss the cell you clicked on you can't just right click again (like you can normally) since the gridcontrol no longer has focus the context menu does, so u have to click to clear the menu then click a third time to bring up the menu for the second time.
Small I know but its annoying me.
Are there any better ways of doing this?
Cheers,
Dave
You can do something like this in the UltraToolbarsManager.BeforeToolDropdown event:
if (e.Tool.Key == "GridContextMenu" && e.SourceControl == this.grid){ UIElement element = this.grid.DisplayLayout.UIElement.ElementFromPoint(Control.MousePosition); UltraGridCell cell = (UltraGridCell)element.GetContext(typeof(UltraGridCell));
PopupMenuTool contextMenu = (PopupMenuTool)e.Tool;
if (cell != null) { contextMenu.Tools["Tool1"].InstanceProps.Visible = DefaultableBoolean.False; contextMenu.Tools["Tool2"].InstanceProps.Visible = DefaultableBoolean.True; // ... } else { // ... }}
Can you please give me the sample code of Mouse_Up event? Thanks in advance
You can also respond to the BeforeToolDropdown event of the toolbars manager. If the key of the tool dropping down is your context menu tool's key and the SourceControl property of the event args is your grid control, you can then update the context menu based on the current mouse position (Control.MousePosition or Cursor.Position).
Hi Dave,
There are a couple of ways to do this.
1) Change the ContextMenuUltra dynamically as the mouse moves over the control. This is not very efficient because you will have to use UIElements to determine what part of the grid the mouse is over and most of the time it will be unneccessary, since the user only right-clicks on the right once in a while.
2) Use the MouseUp event. Note that I say MouseUp, not MouseDown. If you test other Windows applications you will see that they never show a context menu on a MouseDown, it's always a MouseUp. Using MouseDown will cause problems with mouse message because the control will get the MouseDown, but then the MouseUp message gets lost.
This is using your Context Menu Ultra of course
I suppose all I really need is an event on the ultra grid control that fires before poping up the context menu that gives me a mouse pos (to get a UI element from) and access to the menu it it just about to display (so I can hide tools).
I like the idea of all the toolbars and menus using the same tools / icons and all the code is in the same place.