I have a winform application developed in C#. It has a wingrid in it and I have added a context menu to it. This has been working fine for quite a while now, but for some reason suddenly the context menu is displaying when I left-click also. It works fine when I right-click. However when I left-click, the context menu displays in the same position on the grid as it was previously (even if the left click is in a different location).
I can provide more details if needed, but I don't wanna muddy he water. Any ideas what could cause this?
Steve Weeks
Unless the context menu is being intentionally shown (which I doubt is the case), I can't think of any good reason why this should happen. I think we'll probably need more details to understand what's happening.
Thanks for responding Vince. Here are some more of the details:
I have a wingird with a context menu asigned to it. Using the Mouse_Up event, I am detecting whether the pointer is on a datagrid cell, and if so, storing the information from the grid row for later use. Then, on the Context Menu Opening event, I am using the stored information about the row to dynamically enable and disable certain items on the context menu. In this manner the context menu items can be used to perform actions on the specific row in the grid.
I also have a double-click row event handler for the grid. So the user can either double-click a row which causes one function to occur, or right-click which provides a drop-down menu of actions to perform on the row.
This all has been working fine for some time.
I've been making modifications to this code, but not to the context menu stuff, more to the data access functions that load data into the grid.
At some point I noticed that left-clicking on the brought up the context menu. The ight-click stil works as desired, and soo does the double-click. However if a left-click is performed on the grid, the contex menu displays. The behavior is odd in that the menu is not displayed at the point on the grid where the left-click occured, but at the point where the last right-click was performed. If the very first thing I do is left-click on the grid, the menu displays in the top-left corner (since there was no previous right-click). The menu is being built correctly for the row however.
If this is a bug that I can't fix, is there a way I can detect that the click was a left click and prevent the context menu from opening?
Steve
I can't think of any good reason (or any bad reason, for that matter) why the context menu should be displaying. From what you've described, nothing should be triggering it.
The event arguments for the MouseUp event includes e.Button, which tells you which mouse button was clicked. Compare this to values in the System.Windows.Forms.MouseButtons enumeration:
if (e.Button == System.Windows.Forms.MouseButtons.Left) // the left mouse button was clicked
It's not clear to me whether this is a problem in implementation or a bug somewhere; I simply don't have enough information to tell. If you'd like us to take a closer look at what you've got, then please submit a support request and include a sample project; a Developer Support Engineer will then be able to investigate more in-depth.