拖放对象参考VB.Net [英] Drag and Dropping an Object Reference VB.Net
问题描述
我正在尝试交换两个单元格的内容及其映射。为此,我需要将对单元格的引用拖放到字符串值本身。然后,我可以使用此引用来更新字典以及获取值。它允许我进行交换,因为我将引用旧单元格以在其中添加所需的值。
I am trying to 'swap' two cells' contents, and their mappings. To do this, I need to drag and drop a reference to the cell as opposed to the string value itself. I can then use this reference to update a Dictionary as well as get the value. It allows allows me to do the swap as I will have a reference to the old cell to add the value needed in there.
我遇到的问题是我不是确保如何传递单元格引用:
The problem I am having is I am not sure how to pass the cell reference:
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
并在放置事件中:
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
我想做的是可能的吗?我已经完全打破了吗?谢谢:)
Is what I am trying to do possible? Have I completely broken it? Thanks :)
推荐答案
您的 e.Data
是 IDataObject
,不通过 DoDragDrop
发送的值。
Your e.Data
is a IDataObject
, not the value you sent with DoDragDrop
.
要获取发送的值,必须致电 e.Data.GetData(...)
。
To get the value you sent, you must call e.Data.GetData(...)
.
要修复您的代码,请将问题行替换为:
To fix your code, replace the problem line with:
Dim copiedFromCell As DataGridViewCell = _
e.Data.GetData(GetType(DataGridViewTextBoxCell))
(或 DataGridView1.CurrentCell的任何类型
是。)
您可以通过调用 e.Data.GetFormats()
。
You can get a list of types available to be dropped by calling e.Data.GetFormats()
.
这篇关于拖放对象参考VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!