gpt4 book ai didi

vb.net - 在 DataGridView 中获取 "current cell"的 X/Y 坐标?

转载 作者:行者123 更新时间:2023-12-05 08:28:12 26 4
gpt4 key购买 nike

[使用 VB 2010/Winforms]

我有一个包含多个列的 DataGridView。它是未绑定(bind)的,没有连接到任何类型的数据库或任何东西——我只是根据用户输入逐个单元格地填充它。

所以无论如何,DGV 中的一列是“图像”类型 (DataGridViewImageColumn)。

我想要做的是,只要单击其中一个图像单元格,上下文菜单条就会显示在单击图像单元格的确切位置。

这是我到目前为止所得到的......

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

Dim columnName As String = DataGridView1.Columns(e.ColumnIndex).Name
If columnName = "Image" Then
Me.Status_ContextMenuStrip1.Show(Me.DataGridView1.CurrentCell.ContentBounds.Location) ' <-- This isn't right, but I must be close!
End If

End Sub

当我运行上面的代码并单击图像单元格时,会出现上下文菜单,但它出现在屏幕的最左上角。我怎样才能让它出现在被点击的单元格所在的确切位置?我实际上希望它出现在单击的单元格下方,以便它具有与组合框“下拉”类似的视觉效果(而且我知道如何尽快偏移 X 和 Y 坐标把它放在它需要的地方附近)。

谢谢!

最佳答案

试试下面的代码

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim columnName As String = DataGridView1.Columns(e.ColumnIndex).Name

If columnName = "Image" Then
Dim RowHeight1 As Integer = DataGridView1.Rows(e.RowIndex).Height
Dim CellRectangle1 As Rectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)

CellRectangle1.X += DataGridView1.Left
CellRectangle1.Y += DataGridView1.Top + RowHeight1

Dim DisplayPoint1 As Point = PointToScreen(New Point(CellRectangle1.X, CellRectangle1.Y))

ContextMenuStrip1.Show(DisplayPoint1)
End If
End Sub

关于vb.net - 在 DataGridView 中获取 "current cell"的 X/Y 坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17557951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com