Solved

SHOW CONTEXTMENUSTRIP AT CURRENT CELL POSITION

Posted on 2011-03-14
19
799 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
  • 9
  • 9
19 Comments
 
LVL 19

Expert Comment

by:Shahan Ayyub
Comment Utility
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
Comment Utility
Hi Shahan_Dev,

I want to use mouse double click.

Thank you.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
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
Comment Utility
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
Comment Utility
See my second post
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
Your second solution always shown at column header.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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
Comment Utility
No it show at the current cell ...did you change the cell mouse click in just MouseDoubleClick event ...it works fine for me...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
please can you tell me which event are u using??
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
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
Comment Utility
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
Comment Utility
Oops, let me try it.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
It works.

Thank you very much for your help.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
Glad I help you ..!!!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Calculating a BCC (Xor) for a message 13 32
Limiting string to two deciamls 18 30
VB.NET HttpWebRequest 12 30
Copy/Clone an object. 9 10
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now