开发者

VB.NET Drag Drop If Object Implements Interface

开发者 https://www.devze.com 2023-01-14 00:53 出处:网络
I have to be missing something simple, but my brain is about开发者_运维百科 to bleed on this. I have an interface, for the sake of argument called MyInterface.

I have to be missing something simple, but my brain is about开发者_运维百科 to bleed on this.

I have an interface, for the sake of argument called MyInterface.

I Have a Control Class, lets name it "Parent" that implements MyInterface.

I have another Control Class that inherits Parent, called "Child"

I have a final Control Class lets call it "Container", that accepts dragging the parent onto it.

The code in Container for Dragover is simple, like this:

    Protected Overrides Sub OnDragOver(ByVal e As System.Windows.Forms.DragEventArgs)
       If e.Data.GetDataPresent(GetType(Parent)) Then
           e.Effect = DragDropEffects.Move
       End If
       MyBase.OnDragOver(e)
       Refresh()
    End Sub

I want to modify this not to only accept Parent, but to accept ANYthing that implements MyInterface. I can't figure out how the heck to get it to work.

Even more confusing, if I drag the child onto Container, with the code as it is above(checking to see if a Parent was dropped), GetDataPresent always returns false. I would figure it would work, since Child inherited parent.

Ideally, I would have something like this:

    Protected Overrides Sub OnDragOver(ByVal e As System.Windows.Forms.DragEventArgs)
       If e.Data.GetDataPresent(GetType(MyInterface)) Then
           e.Effect = DragDropEffects.Move
       End If
       MyBase.OnDragOver(e)
       Refresh()
   End Sub

But it bombs and GetDataPresent returns false whether I dragged a Parent or a Child. I'm being an idiot somewhere...but I cont know where. Help?


The Data object uses the type as a key in a collection. So it looks for an exact match.

I believe you need to check whether there's an Object available in Data (that is, absolutely any object), and if there is, retrieve it and check whether it supports the interface with TypeOf.

0

精彩评论

暂无评论...
验证码 换一张
取 消