vb.net 如何在 vb 2008 中创建一个图片框并移动它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18256324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create a picturebox in vb 2008 and move it?
提问by Hassan Kanso
I just want to ask a really important question in vb2008 : I'm working on 2D level designer that I just put all the images and the collision rectangles in his place then the program generate the right code. the problem is : I make a button and in his click event it add a new picture box , when it add I have to move it with the mouse , I have the "move" code but like you know it's a new picture box so I can't write the code before the picture box add.(if you doesn't understand I can explain my situation again)
我只想在 vb2008 中问一个非常重要的问题:我正在研究 2D 关卡设计师,我只是将所有图像和碰撞矩形放在他的位置,然后程序生成正确的代码。问题是:我制作了一个按钮,并在他的点击事件中添加了一个新图片框,添加时我必须用鼠标移动它,我有“移动”代码,但就像你知道它是一个新图片框,所以我不能在添加图片框之前写代码。(如果你不明白我可以再解释一下我的情况)
for more clear this is the code :
为了更清楚,这是代码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As 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 to form
Controls.Add(newPictureBox)
goToRight = goToRight + newPictureBox.Width
i += 1
End Sub
and this is the "move picturebox" code(it's for an existed picturbox):
这是“移动图片框”代码(用于现有的图片框):
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim endPoint As 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 Then
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 As Object, ByVal e As 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 As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
IcanMove = False
End Sub
thanks you for reading :)
谢谢你的阅读:)
采纳答案by Raphael Smit
You need to add event handlers for the newly added PictureBox, here is how to do that.
您需要为新添加的PictureBox.添加事件处理程序,这是如何做到的。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NewPictureBox As PictureBox = New PictureBox
NewPictureBox.Parent = Me
NewPictureBox.Location = New Point(100, 100)
Me.Controls.Add(NewPictureBox)
'you can use existing event handlers like your PictureBox1_MouseDown
AddHandler NewPictureBox.MouseUp, AddressOf PictureBox_MouseUp
AddHandler NewPictureBox.MouseDown, AddressOf PictureBox_MouseDown
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
MessageBox.Show("Triggered MouseDown Event")
End Sub
Private Sub PictureBox_MouseUp(sender As Object, e As MouseEventArgs)
MessageBox.Show("Triggered MouseUp Event")
End Sub
(if you also remove the PictureBox's before closing the form be sure to remove the handlers for them)
For more info check AddHandlerand RemoveHandler
(如果您PictureBox在关闭表单之前还删除了's,请确保删除它们的处理程序)
有关更多信息,请查看AddHandler和RemoveHandler

