【问题标题】:Drag and Dropping an Object Reference VB.Net拖放对象引用 VB.Net
【发布时间】:2008-11-26 17:15:46
【问题描述】:

我正在尝试“交换”两个单元格的内容及其映射。为此,我需要拖放对单元格的引用,而不是字符串值本身。然后我可以使用这个引用来更新字典并获取值。它允许我进行交换,因为我将引用旧单元格以在其中添加所需的值。

我遇到的问题是我不确定如何传递单元格引用:

Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
    If e.Button = MouseButtons.Left Then
        DataGridView1.DoDragDrop(DataGridView1.CurrentCell, DragDropEffects.Copy)
    End If

End Sub

在 drop 事件中:

Private Sub DataGridView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop

   'Problem is here -->'Dim copyedFromCell As DataGridViewCell = DirectCast(e.Data(), DataGridViewCell)** 
    Dim copyedFromKey As String = GetMappingForCell(copyedFromCell) 
    Dim thisKey As String = GetMappingForCell(DataGridView1.CurrentCell)
    Dim copyedFromValue As String = copyedFromCell.Value
    Dim thisValue As String = DataGridView1.CurrentCell.Value

    mappings(copyedFromKey) = DataGridView1.CurrentCell
    mappings(thisKey) = copyedFromCell

    DataGridView1.CurrentCell.Value = copyedFromValue
    copyedFromCell.Value = thisValue

End Sub

我正在尝试做的事情可能吗?我完全破坏了吗?谢谢:)

【问题讨论】:

  • 当我从当前单元格拖动时,它会在鼠标单击时被选中,但是当我想拖放到另一行的其他位置时,目标单元格不会被选中,因此当前单元格不会在我想更新它时保留它的引用。有什么想法吗?

标签: .net vb.net reference drag-and-drop object


【解决方案1】:

您的e.DataIDataObject不是您使用DoDragDrop 发送的值。

要获取您发送的值,您必须致电e.Data.GetData(...)

要修复您的代码,请将问题行替换为:

Dim copiedFromCell As DataGridViewCell = _
   e.Data.GetData(GetType(DataGridViewTextBoxCell))

(或任何DataGridView1.CurrentCell 的类型。)

您可以通过调用e.Data.GetFormats() 获取可删除的类型列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多