C# – Disable Right-Click for ContextMenuStrip in DataGridView

cwinforms

I have a DataGridView with a dgv1.CellClick.

The context menu shows up with the cell is clicked with the left mouse button. When this happens it also sets the position to the current cell BUT the contextmenustrip also shows up when I click on the RIGHT mouse button. I want to disable or hide the context menu when the right mouse button is clicked.

I have tried:

    private void dgv1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
            cms1.Hide();
    }

and this did not work.

Does anyone have any advice?

Best Answer

    private MouseButtons e_Button = new MouseButtons();
    private void dgv1_MouseDown(object sender, MouseEventArgs e)
    {
        e_Button = e.Button;
    }

    private void cms1_Opening(object sender, CancelEventArgs e)
    {
        if (e_Button == System.Windows.Forms.MouseButtons.Right)
            e.Cancel = true;
    }
Related Topic