vb.net - Creating Dynamic Controls in VB and having them pre-programmed in different events -


i have project can create picturebox control want every picturebox user creates have events set in place such mouse down , mouse events. since control hasnt been created yet, can't refer in code without getting error , form not being able load because of it. in other words, after user creates picturebox, can move picturebox around screen , draw on it. can create picturebox , move around , draw on , arrange pictureboxes please. ideas? thanks. here code:

private sub addcanvastoolstripmenuitem_click(byval sender system.object, byval e system.eventargs) handles addcanvastoolstripmenuitem.click             dim canvas new picturebox             dim integer = 0             = + 1             canvas.name = "canvas"             canvas.backcolor = color.white             canvas.borderstyle = borderstyle.fixedsingle             canvas.image = nothing             canvas.height = 200             canvas.width = 200             addhandler canvas.mousedown, addressof picturebox1_mousedown             addhandler canvas.mousemove, addressof picturebox1_mousemove             canvas.top = panel2.bottom             canvas.left = panel1.right             controls.add(canvas)         end sub      private sub picturebox1_mousedown(byval sender object, byval e system.windows.forms.mouseeventargs) handles picturebox1.mousedown             if radiobutton1.checked = true                 xpos = cursor.position.x - picturebox1.location.x                 ypos = cursor.position.y - picturebox1.location.y             end if              if radiobutton2.checked = true                 down = true                 if down = true                     picturebox1.creategraphics.fillellipse(mybrush, e.x, e.y, 2, 2)                 end if             end if         end sub          private sub picturebox1_mousemove(byval sender object, byval e system.windows.forms.mouseeventargs) handles picturebox1.mousemove             if radiobutton1.checked = true                 if e.button = windows.forms.mousebuttons.left                     pos = mouseposition                     pos.x = pos.x - xpos                     pos.y = pos.y - ypos                     picturebox1.location = pos                 end if             end if              if down = true                 picturebox1.creategraphics.fillellipse(mybrush, e.x, e.y, 2, 2)             end if         end sub 

but makes want happen canvas happen picturebox1. dont want picturebox1 exist in first place. want them create new picturebox out of events programmed it. user can create new picturebox , move , draw on it.

create events dynamically too, this:

private sub addcanvastoolstripmenuitem_click(byval sender system.object, byval e system.eventargs) handles addcanvastoolstripmenuitem.click dim canvas new picturebox dim integer = 0 = + 1 canvas.name = "canvas" canvas.backcolor = color.white canvas.borderstyle = borderstyle.fixedsingle canvas.image = nothing canvas.height = 200 canvas.width = 200 addhandler canvas.mousedown, addressof pic_mousedown canvas.top = panel2.bottom canvas.left = panel1.right controls.add(canvas) end sub  private sub pic_mousedown(byval sender system.object, byval e system.windows.forms.mouseeventargs)     'do end sub 

Comments