Using a ContainerControl Class, a Canvas to filter droped items

a.k.k. Avoid troubles with Drag and Drop with a Canvas

This is a simple HowTo.

Fire Xojo,
New Project (with the name you want, “ContainerControl Example”, if you like it)

a. Add a ContainerControl (CC) and name it CC_Img,
b. Add a Canvas to that CC_Img and name it C_Image,
c. Open CC_Img, then Open Controls,
d. Click in C_Image to add events and code to it.
e. In CC_Img, add a Property:

Logo_Pict As Picture

Add a File Type Set, name it FT_Imgs and put there some image file types (png, jpg, tif),

C_Image : Add an Open Event and put the code:

// C_Image accept defined images drops only Me.AcceptFileDrop FT_Imgs.All

C_Image : Add a Paint Event and put the code:

[code]// Draw the dropped picture
#PRAGMA Unused areas

If Logo_Pict <> Nil Then
Dim X As Double
Dim Y As Double

// Compute the X, Y values (so the image will be centered…)
X = (C_Image.Width - Logo_Pict.Width) / 2
Y = (C_Image.Height - Logo_Pict.Height) / 2

// Draw the image, centered
g.DrawPicture Logo_Pict, X,Y
End If[/code]

C_Image : Add a DropObject Event and put the code:

[code]// Deals with Picture file drop
#PRAGMA Unused action

Dim LogoFI As FolderItem
Dim Drop_Pict As Picture

// a. Get a local FolderITem reference
If Not Obj.FolderItemAvailable Then
// Stop here
End If
// Work with a local variable
LogoFI = Obj.FolderItem

// b. Load the image in a local reference
Drop_Pict = Picture.Open(LogoFI)
If Drop_Pict = Nil Then
// Stop here
End If

// c. Ask the Canvas to Refresh

Move CC_Img into Window1 (it will be renamed CC_Img1).

Save the project.

Run, drop an image whose file type is implemented in the File Type Set and enjoy.

Not, drop another file (or a folder) into your window (in the Canvas on that window) and enjoy: it will be rejected.

No need to make any filtering in the DropObject Event.