I got a contextmenu in my grid that appears when i right click a cell. The problem i've been having is that when i rightclick a cell while being in editmode the default copy/paste contextmenu appears.
Now i've been able to fire the MouseUp event (and tried MouseDown aswell) using this example:http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=3036
However when i rightclick while being in editmode now i first get the default contextmenu and after i close that (with Esc or clicking somewhere else for example) my contextmenu appears. I just want to disable the default context menu and get my contextmenu to apear right away.
I've been looking around for abit and tried to implement the following code in my form (that contains the UltraGrid):
protected override void WndProc(ref System.Windows.Forms.Message m) { //Suppress the message to display the context menu const int WM_CONTEXTMENU = 0x7b; if (m.Msg != WM_CONTEXTMENU) this.WndProc(ref m); }
Apperantly overriding the WndProc function lets you suppress default windows behaviour but i've not been able to get it to work. I get a stackoverflow on the If statement while loading the form.
What i mostly find weird is that i saw a similair topic on this forum:http://forums.infragistics.com/forums/t/5035.aspx
However he does not have the default copy/paste contextmenu problem.
Thanks in advance.
I've found this thread:
http://stackoverflow.com/questions/266971/disable-datagridview-system-contextmenu
Unfortunately the Ultragrid doesn't have the suggested event. However i guess i don't need it because i can access the same property through the controladded/removed functions. I've tried to add an empty contextmenu like this:
private void dgvOverview_ControlAdded(object sender, ControlEventArgs e) { e.Control.MouseUp += new MouseEventHandler(dgvOverview_MouseUp); e.Control.ContextMenu = new ContextMenu(); } private void dgvOverview_ControlRemoved(object sender, ControlEventArgs e) { e.Control.MouseUp -= new MouseEventHandler(dgvOverview_MouseUp); e.Control.ContextMenu = new ContextMenu(); }
Stil it shows up.
The easiest thing to do would be to set the ContextMenu on the grid control in the MouseMove or MouseDown events. You don't have to set it on the child control explicitly, setting it on the grid before the ContextMenu is displayed will automatically pass it off to the textbox in the cell.
Mike Saltzman said: Unless you are somehow hooking the MouseUp event twice, or maybe hooking it on the child control in addition to the grid, I don't see why the event would fire twice. Did you try looking at the call stack to see where it's coming from each time?
Unless you are somehow hooking the MouseUp event twice, or maybe hooking it on the child control in addition to the grid, I don't see why the event would fire twice.
Did you try looking at the call stack to see where it's coming from each time?
I did bind the event to the grid itself and also to the child control through the ControlAdded event. The calls stacks and the sender parameter are identical both times though (Infragistics.Win.EmbeddableTextBox). If the grid would cause one of the events then i would suspect that would be the sender.
I tried removing/adding the event from the grid aswell with the AfterEnterEditMode / AfterExitEditMode events. No effect either.
Mike Saltzman said:I really think the approach you are taking here is overly complex. If you simply trap MouseMove and detect what cell the mouse is over and then assign the ContextMenu(Strip) to the grid, it will save you a lot of work and a lot of headaches. :)
That would be an option yes. But i still want to do something on the MouseUp event (saving the value and doing some checks on it) so i still have the same problem.
renzov said:That would be an option yes. But i still want to do something on the MouseUp event (saving the value and doing some checks on it) so i still have the same problem.
I don't understand what you mean. If you are showing a ContextMenu, then you really can't do anything in the MouseUp. If you take a look at any application in Windows, you will see that a ContextMenu is never displayed on the MouseDown, it is displayed on the MouseUp. So if you need to do something, like prepare the ContextMenu items or enable/disable them, then you do this in the MouseDown.
Your right the reason i use the MouseUp event is because i used that event to call Show() from the ContextMenuStrip. However thats not really the problem. The problem is still that those events get fired twice for no reason i can see.
Not sure what to tell you there. If you can duplicate this behavior in a small sample project, I will be happy to take a look. It's possible that this is a bug in the grid, but I am not aware of any such issues.
I have made it so that it doesn't really matter that much that it get fired twice so gonna let it rest for now as i have some other things to do. I might make a demo project in the future. Thanks anyway for all the help.
Hi Chris,
I'm not following you. You say the "click cell" event fires before MouseDown? That doesn't sound right. MouseDown would have to fire first, I think.
chris_o_10 said:The mouse event is then triggered but the menu won't be displayed as I have have not clicked the right mouse button.
I'm not sure what you mean by this, either. If you are not clicking the mouse button, then why is click or MouseDown firing?
You may be better off using MouseDown or MouseMove to assign your context menu to the grid using the UltraToolbars SetContextMenu method, rather than ShowPopup. That way the context menu becomes associated with the grid and the grid will handle showing it whenever the system would normally show a context menu.
EDIT: By the way, showing a context menu in MouseDown is not a good idea and can easily cause problems in your application. If you try this out with any Windows application, you will find that it's a standard to show a context menu in the MouseUp, never MouseDown. Using MouseDown means that the control you clicked gets a MouseDown and then MouseUp message goes to the menu and not the control, which can leave the control confused because it got a MouseDown with no MouseUp.
I have now manged to fix this
thanks
Chris
Hi,
sorry for resurrecting this thread however after doing a search on the forum it was the closest match..
I'm too having difficulties applying my own pop up menu when a cell is in edit mode. The menu (only paste option atm) works fine when I right click anywhere on the grid.
This is my current code in the Mouse down event;
private void gvUserData_MouseDown(object sender, MouseEventArgs e)
{
UIElement mousedownUIElement = null;
if (e.Button == MouseButtons.Right) {
mousedownUIElement = gvUserData.DisplayLayout.UIElement.ElementFromPoint(new Point (e.X, e.Y));
}
if (mousedownUIElement != null)
UltraGridCell mCell;
mCell = mousedownUIElement.GetContext(typeof(UltraGridCell)) as UltraGridCell;
if (mCell != null)
gvUserData.ActiveCell = mCell;
toolbarsManager.ShowPopup("UserGridPopup");
I have a method that handles the click cell event and enters edit mode. The mouse event is then triggered but the menu won't be displayed as I have have not clicked the right mouse button. Is there a way that this method could be called again once in edit mode??
Thanks