DataGridView.HitTest(Int32, Int32) 方法

定義

在給定 x 和 y 座標的情況下傳回位置資訊,例如,資料列和資料行的索引。

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

參數

x
Int32

x 座標。

y
Int32

y 座標。

傳回

DataGridView.HitTestInfo

DataGridView.HitTestInfo,包含位置資訊。

範例

下列程式碼範例說明此方法的使用方式。 此範例是屬性參考主題中較大範例的 Columns 一部分。

private DataGridViewCell clickedCell;

private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// If the user right-clicks a cell, store it for use by the shortcut menu.
    if (e.Button == MouseButtons.Right)
    {
        DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
        if (hit.Type == DataGridViewHitTestType.Cell)
        {
            clickedCell =
                dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
        }
    }
}
Private clickedCell As DataGridViewCell

Private Sub dataGridView1_MouseDown(ByVal sender As Object, _
    ByVal e As MouseEventArgs) Handles dataGridView1.MouseDown

    ' If the user right-clicks a cell, store it for use by the 
    ' shortcut menu.
    If e.Button = MouseButtons.Right Then
        Dim hit As DataGridView.HitTestInfo = _
            dataGridView1.HitTest(e.X, e.Y)
        If hit.Type = DataGridViewHitTestType.Cell Then
            clickedCell = _
                dataGridView1.Rows(hit.RowIndex).Cells(hit.ColumnIndex)
        End If
    End If

End Sub

備註

您可以使用這個方法來判斷控制項的 DataGridView 哪個部分位於指定的座標。 例如,您可以呼叫這個方法 - 指定滑鼠點選的座標 - 來判斷所按一下儲存格的資料列和資料行索引,或是按一下標題儲存格或捲軸。

適用於

另請參閱