In my ultragrid, right-clicking on any cell brings up a right-click menu of actions pertinent to that row. We show the menu in the MouseUp event after checking if MouseEventArgs.Button = MouseButton.Right. Unfortunately there's no easy way to get what the user clicked on in the grid.
But there's a ClickCell event that does tell me what an user clicked on. I don't have to do any work to figure out what the user clicked on. It just tells me (with .Cell property). I like this much better than the Mouse* group of events.
Unfortunately there's no way to know if they right-clicked. Is there some other event that I could use, or am I stuck using MouseUp? What's the easiest/clearest way to do this?
Hi Jeffrey,
The best thing to do is use the MouseDown event to store the last mouse button in a variable. Then you can reference that variable inside the Click or CellClick event to know which button was used.
This is true for the Click event of any Windows Forms Control. I'm not sure why Microsoft decided not to provide the mouse button inside the Click event, but I'm sure there's probably some reason.
Mike,
This works for right-click but not left. The order of events is reversed for the left button, so the "CellClick" fires before "MouseDown". How can I determine whether the left button was actually used (and not a side button or other) when clicking a cell?
Oh, I see. :)This approach should work just fine, then.
And like I said... if have cells with editor buttons or dropdown buttons and you wanted to exclude those, all you would have to do is account for the type of uiElement and just bail out if it's that particular type of UIElement.
Thanks for your help Mike, your code snippet worked. I mean buttons on the mouse, i.e. I wanted to determine which mouse button -- left, right, middle, side1, side2 or whatever they're called.
You mentioned something about "side button or other". Not sure exactly what that means. If you are using EditorButtons or DropDown buttons in the cell and you want to exclude those, then you might have to dig into this code a little deeper and check the type of the uiElement and bail out for certain types of elements.
Hi Rory,
I tried this out and I get the same results. Unfortunately, I don't think this is something we can fix. Changing the order of events is a very dangerous breaking change, so even in a case like this where it looks like the event order is clearly wrong, we try to avoid changing it because it will almost certainly break existing applications.
There are a couple of ways you could get around this and get the information you need.
I think the simplest thing for you to do is to simply not use the ClickCell event and just use the MouseDown event of the grid. The only tricky part of that is getting the cell (if any) which was clicked. And that's not very difficult...
private void UltraGrid1_MouseDown(object sender, MouseEventArgs e) { var grid = (UltraGrid)sender; var gridUIElement = grid.DisplayLayout.UIElement; var uiElement = gridUIElement.ElementFromPoint(e.Location); if (null == uiElement) return; var cell = uiElement.GetContext(typeof(UltraGridCell)) as UltraGridCell; if (null == cell) return; Debug.WriteLine(cell.ToString(), "Cell Clicked"); Debug.WriteLine(e.Button, "Mouse button"); }