Hey,

I am not able to debug my code, what I am trying to do is have a button on the form, and clicking that button should create pictureboxes on the fly and those pictureboxes should be drag and droppable anywhere on the form.

What I have so far is
I have a class (buttonarray.vb) which has the code for dynamic picturebox creation, dragdrop methods and a form1.vb which uses this class to create pictureboxes on its form

So far I am able to create as many pictureboxes as needed but not able to drag and drop them as I want

Can some check my code and let me know what is wrong with my drag-drop subroutine?

****************************************************Code for buttonarray class
****************************************************


Imports System.IO

Public Class buttonarray
Inherits System.Collections.CollectionBase

Private ReadOnly HostForm As System.Windows.Forms.Form

'The form's border is 4 Pixels wide
Const FORM_BORDER = 4
'The form's caption bar is 30 Pixels high
Const FORM_CAPTION = 30


Public Function AddNewPicturebox() As System.Windows.Forms.PictureBox

' Create a new instance of the picturebox class.
Dim apicturebox As New System.Windows.Forms.PictureBox()

Dim path As String = "C:\pic1.gif"

' Add the picturebox to the collection's internal list.
Me.List.Add(apicturebox)

' Add the picturebox to the controls collection of the form
' referenced by the HostForm field.
HostForm.Controls.Add(apicturebox)

' Set intial properties for the button object.
apicturebox.Top = Count * 40
apicturebox.Left = 100
apicturebox.Tag = Me.Count
apicturebox.Text = " Picture " & Me.Count.ToString
apicturebox.SizeMode = PictureBoxSizeMode.StretchImage
' Load the picture into the control.
Try
apicturebox.Image = Image.FromFile(path)
Catch ex As Exception
MessageBox.Show(" in the catch " & ex.ToString)
End Try


AddHandler apicturebox.MouseDown, AddressOf Me.apictureboxMousedownHandler
AddHandler HostForm.DragEnter, AddressOf Me.FormDragEnterHandler
AddHandler HostForm.DragOver, AddressOf Me.FormDragOverHandler

Return apicturebox

End Function



Public Sub New(ByVal host As System.Windows.Forms.Form)
HostForm = host
Me.AddNewPicturebox()
End Sub


Default Public ReadOnly Property Item(ByVal Index As Integer) As System.Windows.Forms.PictureBox
Get
Return CType(Me.List.Item(Index), System.Windows.Forms.PictureBox)
End Get
End Property

Private Sub apictureboxMousedownHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

If e.Button = MouseButtons.Left Then
CType(sender, System.Windows.Forms.PictureBox).DoDragDrop(CType(sender, System.Windows.Forms.PictureBox), DragDropEffects.Move)
End If

End Sub


**************Problem area *************************

Private Sub FormDragEnterHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)

e.Effect = DragDropEffects.Move
End Sub


Private Sub FormDragOverHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
Dim MyForm As Form

'To access the properties of the sender object you need to 'dimension an object variablef the sender type (Form in this 'case) and reference the sender object with it.
MyForm = sender

CType(sender, System.Windows.Forms.PictureBox).Left() = e.X - MyForm.Location.X - FORM_BORDER - CType(sender, System.Windows.Forms.PictureBox).Width / 2

CType(sender, System.Windows.Forms.PictureBox).Top() = e.Y - MyForm.Location.Y - FORM_CAPTION - CType(sender, System.Windows.Forms.PictureBox).Height / 2


End Sub

*********end of problem area **************************

End Class
#########################################




***************************************************
Code for form1.vb
***************************************************

Imports System.Drawing.Image



Public Class Form1
Inherits System.Windows.Forms.Form

**************Can ignore this part ******************

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()
MyControlArray = New buttonarray(Me)


'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnAdd As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnAdd = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(528, 216)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.TabIndex = 1
Me.btnAdd.Text = "add button"
'
'Form1
'
Me.AllowDrop = True
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(608, 406)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnAdd})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

**************** End of ignore part ********************


Dim MyControlArray As buttonarray

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

MyControlArray.AddNewPicturebox()
MessageBox.Show(MyControlArray(0).Text)

End Sub

End Class