By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

.HitTestInfo Class

P: 5
I'm trying to give a context menu to my user when they right click on a particular row in my datagrid. I created two functions that pass back the indicies for the row and column. The following code is located in the Mouse_Clicked event point for the datagrid itself. When I right click the first time everything works perfetly, but the next time I right click the context menu comes up but nothing else gets updated. If I left click on the datagrid then right click it behaves normally, so I guess my question is why doesn't the mouse_click event fire correctly when the user right clicks? Here is the code:


Expand|Select|Wrap|Line Numbers
  1.     Private Sub dgridContractors_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgridContractors.MouseClick
  2.         If e.Button = Windows.Forms.MouseButtons.Right Then
  3.             'Hightlight the row
  4.             Me.dgridContractors.Rows(dgridContractors.CurrentRow.Index).Selected = False
  5.             Me.dgridContractors.Rows(NewRowIndex(e.X, e.Y)).Selected = True
  6.  
  7.             With Me.ContextMenuStrip1
  8.                 .Items.Clear()
  9.                 .Items.Add("Delete this record")
  10.                 .Items.Add("Sort by Column """ & dgridContractors.Columns(NewColumnIndex(e.X, e.Y)).HeaderText & """")
  11.             End With
  12.             ContextMenuStrip1.Show(Windows.Forms.Cursor.Position)
  13.         End If
  14.     End Sub
  15.  
  16.     Private Function NewRowIndex(ByVal x As Integer, ByVal y As Integer) As Integer
  17.         Dim info As DataGridView.HitTestInfo = dgridContractors.HitTest(x, y)
  18.         Return info.RowIndex
  19.         info = Nothing
  20.     End Function
  21.  
  22.     Private Function NewColumnIndex(ByVal x As Integer, ByVal y As Integer) As Integer
  23.         Dim info As DataGridView.HitTestInfo = dgridContractors.HitTest(x, y)
  24.         Return info.ColumnIndex
  25.         info = Nothing
  26.     End Function
  27.  
Apr 11 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.