Solved

SHOW CONTEXTMENUSTRIP AT CURRENT CELL POSITION

Posted on 2011-03-14
19
804 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
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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 500 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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