简体   繁体   中英

Context menu not displaying near datagridview cell where right click occurred

I am having a bit of problem with a contextmenu popup. I have a datagridview that I want a user to be able to right click on a cell and get the contextmenu displayed. The user can multi-select continous or non-continuous cells. Right click and get a context menu. I have read through the stack overflow questions and tried out the suggestions and none have worked for me. The context menu does not display near the selected cell.

I am coding behind the datagridview cellmouse down event and checking for right mouse button. Here is my code:

if (e.Button == MouseButtons.Right)
{
    ContextMenu m = new ContextMenu();
    MenuItem mi = new MenuItem("Set Online");
    mi.Click += setOnlineItemCell_Click;
    m.MenuItems.Add(mi);

    MenuItem mi2 = new MenuItem("Set Offline");
    mi2.Click += setOfflineItemCell_Click;
    m.MenuItems.Add(mi2);

    m.Show(machineGrid, new Point(e.X, e.Y));
}

I cannot pass just a single parameter to the Show method as it complains it can't find it (even though MSDN says you can).

Any help in getting the contextmenu to display close to the cell would be greatly appreciated.

C#,Visual Studio 2015 Professional, .NET 4.5.2

Getting the context menu to display next to the last “selected” cell is doable; however, it is not going to be very user friendly. In most cases, when a user “right-clicks” on something a context menu is expected to appear “underneath” wherever the mouse “pointer” was when the user “right-clicked”. What you are describing is moving the context menu “away” from where the mouse “arrow” is located. I am confident most users will not be expecting this.

I am guessing from what you posted… that the user selects multiple cells in the grid, then the user “right-clicks” somewhere on the grid. This would display a context menu (underneath the MOUSE and not move the menu to some selected cell). The context menu would display the “OffLine/OnLine” menu items. Then after the user selects “Online/Offline”, some method would use the “selected” cells in the grid as data to set the cells online or offline. This is about as much as I can decipher from your question.

To help, below are some suggestions. The last line of code in the post….

m.Show(machineGrid, new Point(e.X, e.Y));

The above line moves the context menu to the grids X, Y values using the UI coordinates. This appears to have nothing to do with which cells are “selected”. Yet this will move the context menu somewhere above where the user right clicked... forcing the user to “chase” the context menu.

Lastly, I question the use of the “CellMouseDown” event to do this context menu logic. The DataGridView has its own “ContextMenuStrip” which you can set. Using the DataGridView's ContextMenuStrip may be an easier approach. Below is an example of setting a DataGridView named dgv_PlayerPool with a ContextMenuStrip as described.

private void SetContexMenu() {
  ContextMenuStrip cms = new ContextMenuStrip();
  cms.Items.Add("Set OnLine", null, setOnline_Click);
  cms.Items.Add("Set OffLine", null, setOffline_Click);
  dgv_PlayerPool.ContextMenuStrip = cms;
}

The items add line above, takes a string to display in the menu, an image and finally an event to call when clicked.

The events fired when the user selects a context menu item.

private void setOffline_Click(object sender, EventArgs e) {
  MessageBox.Show("SetOffLine");
}
private void setOnline_Click(object sender, EventArgs e) {
  MessageBox.Show("SetOnLine");
}

Hope this makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM