DataGrid.HitTest 方法

定義

取得畫面指定點中 DataGrid 控制項的相關資訊。

多載

HitTest(Int32, Int32)

使用傳遞至這個方法的 x 和 y 座標來取得資訊,例如方格上按下點的資料列和資料行編號。

HitTest(Point)

取得使用指定 Point 方格的相關資料,例如方格上按下點的資料列和資料行編號。

HitTest(Int32, Int32)

使用傳遞至這個方法的 x 和 y 座標來取得資訊,例如方格上按下點的資料列和資料行編號。

public:
 System::Windows::Forms::DataGrid::HitTestInfo ^ HitTest(int x, int y);
public System.Windows.Forms.DataGrid.HitTestInfo HitTest (int x, int y);
member this.HitTest : int * int -> System.Windows.Forms.DataGrid.HitTestInfo
Public Function HitTest (x As Integer, y As Integer) As DataGrid.HitTestInfo

參數

x
Int32

座標的水平位置。

y
Int32

座標的垂直位置。

傳回

DataGrid.HitTestInfo,包含方格按下區域的相關資訊。

範例

下列程式碼範例會在 HitTest 使用者按一下方格時所發生的 事件中使用 方法。

Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim myGrid As DataGrid = CType(sender, DataGrid)
    Dim hti As System.Windows.Forms.DataGrid.HitTestInfo
    hti = myGrid.HitTest(e.X, e.Y)
    Select Case hti.Type
    Case System.Windows.Forms.DataGrid.HitTestType.None 
       Console.WriteLine("You clicked the background.")
    Case System.Windows.Forms.DataGrid.HitTestType.Cell 
       Console.WriteLine("You clicked cell at row " & hti.Row & ", col " & hti.Column)
    Case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader
       Console.WriteLine("You clicked the column header for column " & hti.Column)
    Case System.Windows.Forms.DataGrid.HitTestType.RowHeader 
       Console.WriteLine("You clicked the row header for row " & hti.Row)
    Case System.Windows.Forms.DataGrid.HitTestType.ColumnResize
       Console.WriteLine("You clicked the column resizer for column " & hti.Column)
    Case System.Windows.Forms.DataGrid.HitTestType.RowResize 
       Console.WriteLine("You clicked the row resizer for row " & hti.Row)
    Case System.Windows.Forms.DataGrid.HitTestType.Caption
       Console.WriteLine("You clicked the caption")
    Case System.Windows.Forms.DataGrid.HitTestType.ParentRows 
       Console.WriteLine("You clicked the parent row")
    End Select
 
 End Sub

備註

HitTest搭配 DataGrid.HitTestInfo 控制項的 方法 System.Windows.Forms.DataGrid ,可用來判斷使用者已按一下的控制項哪個 System.Windows.Forms.DataGrid 部分。 DataGrid.HitTestInfo包含已按一下之方格的資料列、資料行和一部分。 此外, Type 屬性會 DataGrid.HitTestType 傳回列舉。

方法 HitTest 會採用控制項的 DragDropDragEnterDragOver 、、 MouseDownMouseMoveMouseUp 和 事件所提供的 System.Windows.Forms.DataGrid x 和 MouseWheel y 引數。

另請參閱

適用於

HitTest(Point)

取得使用指定 Point 方格的相關資料,例如方格上按下點的資料列和資料行編號。

public:
 System::Windows::Forms::DataGrid::HitTestInfo ^ HitTest(System::Drawing::Point position);
public System.Windows.Forms.DataGrid.HitTestInfo HitTest (System.Drawing.Point position);
member this.HitTest : System.Drawing.Point -> System.Windows.Forms.DataGrid.HitTestInfo
Public Function HitTest (position As Point) As DataGrid.HitTestInfo

參數

position
Point

Point,代表單一 X、Y 座標。

傳回

DataGrid.HitTestInfo,包含方格的指定資訊。

範例

下列程式碼範例會在 HitTest 使用者按一下方格時,使用 中的 方法。

Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim hti As DataGrid.HitTestInfo
    hti = grid.HitTest(New Point(e.X, e.Y))
    Select Case hti.Type
    Case System.Windows.Forms.DataGrid.HitTestType.None 
       Console.WriteLine("You clicked the background.")
    Case System.Windows.Forms.DataGrid.HitTestType.Cell 
       Console.WriteLine("You clicked cell at row " & hti.Row & ", col " & hti.Column)
    Case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader
       Console.WriteLine("You clicked the column header for column " & hti.Column)
    Case System.Windows.Forms.DataGrid.HitTestType.RowHeader 
       Console.WriteLine("You clicked the row header for row " & hti.Row)
    Case System.Windows.Forms.DataGrid.HitTestType.ColumnResize
       Console.WriteLine("You clicked the column resizer for column " & hti.Column)
    Case System.Windows.Forms.DataGrid.HitTestType.RowResize 
       Console.WriteLine("You clicked the row resizer for row " & hti.Row)
    Case System.Windows.Forms.DataGrid.HitTestType.Caption
       Console.WriteLine("You clicked the caption")
    Case System.Windows.Forms.DataGrid.HitTestType.ParentRows 
       Console.WriteLine("You clicked the parent row")
    End Select
 End Sub

備註

HitTest搭配 DataGrid.HitTestInfo 控制項的 方法 System.Windows.Forms.DataGrid ,可用來判斷使用者已按一下的控制項哪個 System.Windows.Forms.DataGrid 部分。 DataGrid.HitTestInfo包含已按一下之方格的資料列、資料行和一部分。 此外, Type 屬性會 DataGrid.HitTestType 傳回列舉。

方法 HitTest 會採用控制項的 DragDropDragEnterDragOver 、、 MouseDownMouseMoveMouseUp 和 事件所提供的 System.Windows.Forms.DataGrid x 和 MouseWheel y 引數。

另請參閱

適用於