Solved

display contextmenu at cursor position

Posted on 2011-03-19
5
1,664 Views
Last Modified: 2012-06-27
I need to display a menu at cursor position when user rigth click in a cell in a datagridview.
I'm including my code, the menu is displayed at top of the datagridview (dgv1), not at cursor position.
Thanks.
Private Sub dgv1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv1.CellMouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim p As New Point(e.X, e.Y)
            mnuOrdenes.Show(dgv1, p)
        End If
    End Sub

Open in new window

0
Comment
Question by:BemboX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 35173009
Why not simply set the "ContextMenuStrip" property of the DataGriView to "mnuOrdenes"? That will do exactly as you require.
0
 

Author Comment

by:BemboX
ID: 35173057
Because it displays the menu although the datagridview is empty (without rows) or the user clicks outside datagrid cells.
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 35173098
In that case, you can use the ContextMenuStrip's Opening event to perform a hittest on the DGV to determine where it is clicked, and if not a cell, cancel the context menu.

    Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
        Dim ht As DataGridView.HitTestInfo = DataGridView1.HitTest(Cursor.Position.X, Cursor.Position.Y)
        If ht.Type <> DataGridViewHitTestType.Cell Then
            e.Cancel = True
        End If
    End Sub

Open in new window


Wayne
0
 
LVL 5

Expert Comment

by:samml
ID: 35174996
Your problem is that the coordinates you get using e are relative and relate to the cell, not the GDV.
0
 
LVL 5

Accepted Solution

by:
samml earned 500 total points
ID: 35175003
Try this:
        If e.Button = Windows.Forms.MouseButtons.Right Then
            ContextMenuStrip1.Show(DataGridView1, Point.Add(DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location, e.Location))
        End If

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question