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

标签 vb.net winforms visual-studio-2010 datagridview

[使用 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/

相关文章:

vb.net - 无法完全清除 Winforms ComboBox 中的项目

arrays - 如何将字节值拆分为多个较小的字节值?

.net - 当已经在正确的线程中时,Form.Invoke 怎么样?

windows - Windows 上 "my computer"文件夹的路径是什么?

c# - 将 Web 应用程序转换为网站

visual-studio-2010 - 将 PostgreSQL 安装程序添加到我自己的应用程序安装程序

vb.net - 将文件从计算机传输到 Azure VM

mysql - odbcException 未处理

c# - 无法在 C# Windows 窗体中触发 MouseWheel 事件

wcf - Visual Studio 2010 SharePoint 可替换参数未被替换