2015-08-31 123 views
0

目前我有一个DataGridView,用户可以用一组UserControls来填充它。我在窗体内部为这些控件附加了control.MouseDown事件的处理程序。如何获取control.MouseDown上的DataGridView中单元格的位置?

我遇到的问题是获取单元格的x和y索引。我需要能够获得列和行的价值,但我所尝试的是不工作。

这是相关的代码片段。

Private Sub MachineTaskMouseDownHandler(sender As Object, e As MouseEventArgs) Handles eventClass.MachineTaskAttemptMove 
    Debug.WriteLine("frmSchedule.MachineTaskMouseDownHandler: Raw: " & e.Location.ToString & " " & Me.ScheduleDataGridView.HitTest(e.X, e.Y).ColumnIndex & " " & Me.ScheduleDataGridView.HitTest(e.X, e.Y).RowIndex & " " & Me.ScheduleDataGridView.HitTest(e.X, e.Y).ColumnX & " " & Me.ScheduleDataGridView.HitTest(e.X, e.Y).RowY) 
    Dim p2 As Point = Me.ScheduleDataGridView.PointToClient(e.Location) 
    Debug.WriteLine("frmSchedule.MachineTaskMouseDownHandler: ToClient: " & p2.ToString & Me.ScheduleDataGridView.HitTest(p2.X, p2.Y).ColumnIndex & " " & Me.ScheduleDataGridView.HitTest(p2.X, p2.Y).RowIndex & " " & Me.ScheduleDataGridView.HitTest(p2.X, p2.Y).ColumnX & " " & Me.ScheduleDataGridView.HitTest(p2.X, p2.Y).RowY) 
    Dim p3 As Point = Me.ScheduleDataGridView.PointToScreen(e.Location) 
    Debug.WriteLine("frmSchedule.MachineTaskMouseDownHandler: ToScreen: " & p3.ToString & Me.ScheduleDataGridView.HitTest(p3.X, p3.Y).ColumnIndex & " " & Me.ScheduleDataGridView.HitTest(p3.X, p3.Y).RowIndex & " " & Me.ScheduleDataGridView.HitTest(p3.X, p3.Y).ColumnX & " " & Me.ScheduleDataGridView.HitTest(p3.X, p3.Y).RowY) 
    Dim ht As DataGridView.HitTestInfo = Me.ScheduleDataGridView.HitTest(p.X, p.Y) 
    from = New Point(ht.ColumnIndex, ht.RowIndex) 
    Debug.WriteLine("frmSchedule.MachineTaskMouseDownHandler: Mouse down on control at " & from.ToString) 

上述代码不一致。根据我在单元格中点击的位置,输出会发生变化。例如,当输出点击细胞的左上角(0,0):

frmSchedule.MachineTaskMouseDownHandler: Raw: {X=10,Y=14} -1 -1 1 1 
frmSchedule.MachineTaskMouseDownHandler: ToClient: {X=-228,Y=-92}-1 -1 -1 -1 
frmSchedule.MachineTaskMouseDownHandler: ToScreen: {X=248,Y=120}1 1 197 95 
frmSchedule.MachineTaskMouseDownHandler: Mouse down on control at {X=-1,Y=-1} 

相比于点击相同的确切细胞的右下方:

frmSchedule.MachineTaskMouseDownHandler: Raw: {X=148,Y=53} 0 0 42 22 
frmSchedule.MachineTaskMouseDownHandler: ToClient: {X=-115,Y=-78}-1 -1 -1 -1 
frmSchedule.MachineTaskMouseDownHandler: ToScreen: {X=411,Y=184}2 2 352 168 
frmSchedule.MachineTaskMouseDownHandler: Mouse down on control at {X=0,Y=0} 

而只是为了完整的缘故,这里是小区的右下角做鼠标按下的结果(1,0):基于价值

frmSchedule.MachineTaskMouseDownHandler: Raw: {X=135,Y=61} 0 0 42 22 
frmSchedule.MachineTaskMouseDownHandler: ToClient: {X=-103,Y=-45}-1 -1 -1 -1 
frmSchedule.MachineTaskMouseDownHandler: ToScreen: {X=373,Y=167}2 1 352 95 
frmSchedule.MachineTaskMouseDownHandler: Mouse down on control at {X=0,Y=0} 

我真的很需要得到小区位置(行/列索引)来自MouseEventArgs。

+0

很抱歉,但它是不是从你的企图昭然若揭:你想要得到的x,y位置给定单元格(相对于DataGridView或主表单)或关联的行,col索引? – varocarbas

+0

@varocarbas对不起,缺乏清晰度。我希望找到单元格的行/列索引。 – JPeroutek

+0

谢谢(取消标记)。你所包含的代码(你的测试和失败)对于证明你已经付出了一些努力是有用的(什么是好的);但这个问题并不太相关。发布代码的相关部分,比如如何定义DataGridView以及为什么CellClick事件不适用于您的情况。 – varocarbas

回答

0

我想我有你在找什么。下面的代码是一个新项目;在Form上放下两个DataGridView控件并用假数据填充它们。请看下面。

Public Class Form1 

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 

     'Remove the added handlers don't forget... 
     For Each ctrl As Control In Me.Controls 
      If TypeOf ctrl Is System.Windows.Forms.DataGridView Then 
       RemoveHandler ctrl.MouseClick, AddressOf GetDataGridViewCellInfo 
      End If 
     Next 

    End Sub 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 

     'Test Data ONLY' 
     Me.DataGridView1.Columns.Add("FirstName", "First Name") 
     Me.DataGridView1.Columns.Add("LastName", "Last Name") 

     Me.DataGridView1.Rows.Add("Marilyn", "Manson") 
     Me.DataGridView1.Rows.Add("Bobby", "Grants") 
     Me.DataGridView1.Rows.Add("Shirley", "Thunderpots") 

     'Add any handlers you need... 
     For Each ctrl As Control In Me.Controls 
      If TypeOf ctrl Is System.Windows.Forms.DataGridView Then 
       AddHandler ctrl.MouseClick, AddressOf GetDataGridViewCellInfo 
      End If 
     Next 

    End Sub 

    Private Sub GetDataGridViewCellInfo(ByVal sender As Object, ByVal e As MouseEventArgs) 
     Try 
      Dim dgv As System.Windows.Forms.DataGridView = DirectCast(sender, System.Windows.Forms.DataGridView) 
      If dgv IsNot Nothing AndAlso dgv.CurrentCell IsNot Nothing Then 
       'The form was just so I could show it by the cell ONLY... 
       Dim frm As New Form 
       frm.StartPosition = FormStartPosition.Manual 
       Dim RowHeight1 As Integer = DataGridView1.Rows(dgv.CurrentCell.RowIndex).Height 
       Dim CellRectangle1 As Rectangle = dgv.GetCellDisplayRectangle(dgv.CurrentCell.ColumnIndex, dgv.CurrentCell.RowIndex, False) 
       'Uncomment the two pieces at the end to show whatever you need at that point... 
       CellRectangle1.X += dgv.Left '+ CellRectangle1.Width 
       CellRectangle1.Y += dgv.Top '+ RowHeight1 

       Dim DisplayPoint1 As Point = PointToScreen(New Point(CellRectangle1.X, CellRectangle1.Y)) 
       frm.Left = DisplayPoint1.X 
       frm.Top = DisplayPoint1.Y 
       frm.ShowDialog() 
       frm.Dispose() 

       MsgBox("Column Index: " & dgv.CurrentCell.ColumnIndex & " Row Index: " & dgv.CurrentCell.RowIndex & Environment.NewLine & "Cell Location = X:" & DisplayPoint1.X & " Y:" & DisplayPoint1.Y) 
      End If 

     Catch ex As Exception 

     End Try 

    End Sub 

End Class 

我在代码中加入了一些小评论。我用一个Form填充,所以我知道它发生在正确的地方。您可以看到反映上面代码的图像。

P.S.这是经得起考验的

enter image description here

enter image description here

enter image description here

enter image description here

相关问题