vb.net - how to create a picturebox in vb 2008 and move it? -
i want ask important question in vb2008 : i'm working on 2d level designer put images , collision rectangles in place program generate right code. problem : make button , in click event add new picture box , when add have move mouse , have "move" code know it's new picture box can't write code before picture box add.(if doesn't understand can explain situation again)
for more clear code :
private sub button2_click(byval sender system.object, byval e system.eventargs) handles button2.click dim integer newpicturebox.image = image.fromfile("c:\users\hp\desktop\ground.bmp") newpicturebox.name = "image" & (i) newpicturebox.visible = true newpicturebox.top = 200 newpicturebox.width = 100 newpicturebox.height = 50 newpicturebox.left = 100 + gotoright newpicturebox.sizemode = pictureboxsizemode.autosize 'add control form controls.add(newpicturebox) gotoright = gotoright + newpicturebox.width += 1 end sub
and "move picturebox" code(it's existed picturbox):
private sub timer1_tick(byval sender system.object, byval e system.eventargs) handles timer1.tick dim endpoint integer = val(textbox1.text) label1.text = "x: " & mouseposition.x - (location.x + 8) label2.text = "y: " & mouseposition.y - (location.y + 29) radiobutton1.left = endpoint + radiobutton2.left panel1.left = 0 'move picture code : if icanmove = true picturebox1.left = mouseposition.x - (location.x + 8) - differencex picturebox1.top = mouseposition.y - (location.y + 29) - differencey end if end sub private sub picturebox1_mousedown(byval sender object, byval e system.windows.forms.mouseeventargs) handles picturebox1.mousedown icanmove = true differencex = (mouseposition.x - (location.x + 8)) - picturebox1.left differencey = (mouseposition.y - (location.y + 29)) - picturebox1.top end sub private sub picturebox1_mouseup(byval sender object, byval e system.windows.forms.mouseeventargs) handles picturebox1.mouseup icanmove = false end sub
thanks reading :)
you need add event handlers newly added picturebox
, here how that.
private sub form1_load(sender object, e eventargs) handles mybase.load dim newpicturebox picturebox = new picturebox newpicturebox.parent = me newpicturebox.location = new point(100, 100) me.controls.add(newpicturebox) 'you can use existing event handlers picturebox1_mousedown addhandler newpicturebox.mouseup, addressof picturebox_mouseup addhandler newpicturebox.mousedown, addressof picturebox_mousedown end sub private sub picturebox_mousedown(sender object, e mouseeventargs) messagebox.show("triggered mousedown event") end sub private sub picturebox_mouseup(sender object, e mouseeventargs) messagebox.show("triggered mouseup event") end sub
(if remove picturebox
's before closing form sure remove handlers them)
more info check addhandler , removehandler
Comments
Post a Comment