views:

66

answers:

1

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?

A: 

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.

GSerg
Ahh, I had no idea it worked like that. I always thought it did some sort of check internally like TryCast, opposed to a lookup. Good to know. Yea, checking the object is easy enough, but would I go about getting just the plain object? GetData() demands a type, and passing MyInterface or even System.Object to it returns nothing. I tried it like: e.Data.GetData(GetType(MyInterface))
instantmusic
@instantmusic To get a plain object, you need to put a plain object in the first place :) `DataObject.SetData(GetType(Object), your_object)`
GSerg
Close, but no cigar(unless I just dont understand). SetData just changes the data in e.Data, but I already have info in there from a DoDragDrop elsewhere. GetData though, always returns nothing unless I tell it the exact type to receive, which I naturally dont want to do since I'm checking for MyInterface instead of type.Sure enough the drag drop info is getting to the sub though.
instantmusic
@instantmusic Yes, I'm talking about the `DoDragDrop` bit. That `SetData` is for the Data object you prepare for DoDragDrop, not for the `e.Data`.
GSerg
Ahh, I think I know what you're getting at. So I tried this in my DoDragDrop: MyBase.DoDragDrop(New DataObject(GetType(MyInterface).ToString, Me), DragDropEffects.All)It worked! If I understand right, using the New(format,object) is the same as declaring the DataObject, then using SetData. Either way, its working now....I was just passing the raw Object in DoDragDrop without using DataObject...heh. Much thanks for your help on this.
instantmusic