?
Solved

SHOW CONTEXTMENUSTRIP AT CURRENT CELL POSITION

Posted on 2011-03-14
19
Medium Priority
?
811 Views
Last Modified: 2012-05-11
Hi All,

I want to achieve above task.

What should I do ?

Thank you.
Private Sub dgvHeader_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvHeader.CellMouseDoubleClick

        Me.ContextMenuStrip1.Show(dgvHeader, New Point(dgvHeader.Width / 2, _
                                                dgvHeader.CurrentCell.GetContentBounds(e.RowIndex).Y))
    End Sub

Open in new window

0
Comment
Question by:emi_sastra
[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
  • 9
  • 9
19 Comments
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 35141377
Hi!

Adding this to your Form_Load Event will do the job:
Me.DataGridView1.ContextMenuStrip = ContextMenuStrip1

Open in new window


Whenever user will press the right mouse button, menu will be show up at the current cell.

In case, this solution doesn't help you. Please elaborate more.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35142550
Hi Shahan_Dev,

I want to use mouse double click.

Thank you.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143028
Hi....
try this in your celldouble Click event ....just adjust your coordinates according to your needs..
       
 Dim x As Integer
        Dim y As Integer
        x = Me.MyDataGridView.CurrentCell.ContentBounds.Left    ' or the  "x" position you need
        y = Me.My.CurrentCell.ContentBounds.Right  ' or the  "y" position you need
        Dim xy As New Point(x, y)
        ContextMenuStrip1.Show(Me, xy)

Open in new window


Hope it helps
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143097
Sorry My first post contains mistakes.....This is what you want (Maybe).....(just use Mouse Double click)

Private Sub MyDataGridView_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyDataGridView.MouseDoubleClick
        Dim hitTestInfo As DataGridView.HitTestInfo
        If e.Button = MouseButtons.Left Then
            hitTestInfo = MyDataGridView.HitTest(e.X, e.Y)
                    If hitTestInfo.Type = DataGridViewHitTestType.Cell Then
                ContextMenu1.Show(MyDataGridView, New Point(e.X, e.Y))
            End If
        End If
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143121
Hi jtoutou,

It always show at header row position, should be at the current cell position.

Thank you.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143143
See my second post
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143144
Your second solution always shown at column header.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143154
Hi Shahan_Dev,

The bad news using your solution is when we click outside the grid, the menustrip still shown.

Thank you.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143157
No it show at the current cell ...did you change the cell mouse click in just MouseDoubleClick event ...it works fine for me...
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143173
emi my solution works ...note tha you have to left double click your mouse...
also check the event ..is mouse doubleclick...
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143198
Please see below code.

Private Sub dgvHeader_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvHeader.CellMouseDoubleClick

   
        Me.ContextMenuStrip1.Show(dgvHeader, New Point(e.X, e.Y))


    End Sub

Anything wrong ?

Thank you.
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 2000 total points
ID: 35143215
just copy the code exactly as it is...and paste it to your code....
Private Sub dgvHeader_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvHeader.MouseDoubleClick
        Dim hitTestInfo As DataGridView.HitTestInfo
        If e.Button = MouseButtons.Left Then
            hitTestInfo = dgvHeader.HitTest(e.X, e.Y)
                    If hitTestInfo.Type = DataGridViewHitTestType.Cell Then
                ContextMenuStrip1.Show(dgvHeader, New Point(e.X, e.Y))
            End If
        End If
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143225
With FullRowSelect of Selection Mode, your code will not work at all.

With the simple code above then it shows at header column.

Thank you.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143248
please can you tell me which event are u using??
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143258
Also with selection mode to full row select it works....check again once more "dgvHeader_MouseDoubleClick" not CellDoubleClick
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143262
I have tried both

Private Sub dgvHeader_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvHeader.CellMouseDoubleClick

   
        Me.ContextMenuStrip1.Show(dgvHeader, New Point(e.X, e.Y))


    End Sub

1. Complete code, with FullRowSelect of Selection Mode property, your code will not work at all.
2. Simple code, then it shows at header column.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143265
Oops, let me try it.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 35143303
It works.

Thank you very much for your help.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 35143314
Glad I help you ..!!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

741 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