{Solved}Help me change location for generated picturebox
Hello,
I'm trying to build a program that generates pictureboxes (image from source) and makes them movable whenever I want.
The problem is that I can't refer to their location and change it.
Here's the code:
Code:
Public Class Form1
Dim point As New System.Drawing.Point
Dim X, Y As Integer
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(200, 200)
NewPictureBox.BackgroundImage = BackgroundImage.FromFile("C:\users\kubau2\desktop\temp\9fot.jpg")
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
AddHandler NewPictureBox.MouseMove, AddressOf PictureBox_MouseMove
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
X = Control.MousePosition.X - PictureBox.Location.X
Y = Control.MousePosition.Y - PictureBox.Location.Y ' Here's the problem. How to make new pictureboxes move, as VB can't find any referral to "PictureBox"
End Sub
Private Sub PictureBox_MouseUp(sender As Object, e As MouseEventArgs)
End Sub
Private Sub PictureBox_MouseMove(sender As Object, e As MouseEventArgs)
point = Control.MousePosition
point.X = point.X - (X)
point.Y = point.Y - (Y)
PictureBox.Location = point 'Same here. Picturebox isn't recognized.
End Sub
End Class
I've also found some other code that does the same thing:
Code:
Public Class Form1
Dim lastfile As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ofd As New OpenFileDialog
ofd.Filter = ("jpgs |*.jpg")
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
lastfile = ofd.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Static pbc As Integer = 0
Dim MyPictureBox As New PictureBox
Try
MyPictureBox.Image = Image.FromFile(lastfile)
Catch ex As Exception
End Try
MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
MyPictureBox.Location = New System.Drawing.Point(pbc * 128, 70)
MyPictureBox.Size = New System.Drawing.Size(126, 136)
MyPictureBox.BorderStyle = BorderStyle.Fixed3D
MyPictureBox.Name = "PictureBox" & pbc.ToString
AddHandler MyPictureBox.Click, AddressOf MyPictureBox_Click
AddHandler MyPictureBox.MouseHover, AddressOf MyPictureBox_MouseOver
' AddHandler MyPictureBox.MouseDown, AddressOf MyPictureBox_MouseMove
' AddHandler MyPictureBox.MouseMove, AddressOf MyPictureBox_MouseMove
Me.Controls.Add(MyPictureBox)
pbc += 1
End Sub
Private Sub MyPictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim PB As PictureBox = CType(sender, PictureBox)
Select Case PB.Name.ToString
Case "PictureBox0"
MessageBox.Show("PictureBox0")
Case "PictureBox1"
MessageBox.Show("PictureBox1")
Case "PictureBox2"
MessageBox.Show("PictureBox2")
Case "PictureBox3"
MessageBox.Show("PictureBox3")
Case "PictureBox4"
MessageBox.Show("PictureBox4")
Case "PictureBox5"
MessageBox.Show("PictureBox5")
End Select
End Sub
Private Sub MyPictureBox_MouseOver(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim PB As PictureBox = CType(sender, PictureBox)
Select Case PB.Name.ToString
Case "PictureBox0"
MessageBox.Show("PictureBox0")
End Select
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim PB As PictureBox = CType(sender, PictureBox)
' Select Case PB.Name.ToString
Select Case "PictureBox0"
Case "PictureBox0"
MessageBox.Show("PictureBox0")
' PictureBox.Location = New System.Drawing.Point(pbc * 128, 70)
End Select
End Sub
End Class
And here's link for the first program: https://app.box.com/s/11quxsa3dkvoo53swvm6
Are here some more experienced users that can help me to solve this problem?
Of course You may change the code totally if You want. Just to make it work.
The location may change even on button click.
Edit:
I've found the code responsible for that.
I just had to use in the 2nd code under the case this line:
PB.Location = point
That's it. It works. Still a little bit bad, as the location is like 100px down, but it shouldn't be hard to fix.
Re: Help me change location for generated picturebox
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
What do you reckon that variable is there for? And why is this line ..
AddHandler NewPictureBox.MouseDown, AddressOf PictureBox_MouseDown
... included in every creation of a PictureBox?
Quote:
Of course You may change the code totally if You want. Just to make it work.
Gee. How generous. Think we'll pass.
Re: Help me change location for generated picturebox
Quote:
Originally Posted by
dunfiddlin
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
What do you reckon that variable is there for? And why is this line ..
AddHandler NewPictureBox.MouseDown, AddressOf PictureBox_MouseDown
... included in every creation of a PictureBox?
Gee. How generous. Think we'll pass.
1) I don't know. I don't understand everything in vb yet (especially stuff like this), as I'm not a professional programmer. Please keep in mind I have found this code, I haven't written it myself.
2)Every? Really? Where?
3)Thanks, You have really helped me. Much appreciated, without You my program wouldn't even start.
And for real now. I thought this is the place to help users solve problems like this, not to say:"You have a problem, do something with this".
If I knew how to do this I WOULDN'T start this thread.
So please, try to do more than just talking.
Re: {Solved}Help me change location for generated picturebox
Quote:
Please keep in mind I have found this code, I haven't written it myself.
But that's precisely what I was keeping in mind! Those who cut and paste without any attempt to understand what it is they have are the bane of our lives here.
Quote:
not to say:"You have a problem, do something with this".
Sorry. What I was saying was, "Here are two fundamental concepts without an understanding of which you will make no genuine progress in VB programming. Look them up!" Apologies if I was in any way unclear about it.
Re: {Solved}Help me change location for generated picturebox
Dim x As New Windows.Forms.PictureBox
x.Image.Save("filename")