Results 1 to 5 of 5

Thread: {Solved}Help me change location for generated picturebox

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    22

    Question {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.
    Last edited by kubau2; Oct 4th, 2013 at 01:12 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width