Results 1 to 19 of 19

Thread: Dynamic Pictureboxs

  1. #1

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Dynamic Pictureboxs

    Hey guys, im trying to make a program that consists of two forms.
    One 'toolbox' form, and one 'pallet' form. The toolbox has components that you drag onto the pallet form. Once you drag them onto the pallet form, you should be able to move around the pictureboxes. Unfortunatley, since they are dynamic controls, i have no way of checking what picturebox you click on when there are multiple pictureboxes on the pallet at one time. they are in an arraylist, however i dont know how to check on my mouse_down routine what array index the picturebox is. any ideas?

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Dynamic Pictureboxs

    You don't need to worry about array indices etc... all events comes with a standard pair of arguments - "sender" and "e", for example

    Code:
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    
        End Sub
    sender is a reference to whatever object raised the event, so you can just use "sender" to manipulate the picture box directly (you will probably want to cast from type object to picturebox first though.

    Here's an example :

    Code:
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    
            Dim MyPb As PictureBox = CType(sender, PictureBox)
    
            MyPb.Visible = False
    
        End Sub

  3. #3

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: Dynamic Pictureboxs

    ok this is wat ive been trying to use. problem is, all of my pictureboxs are stored in an arraylist. how do i tell what one im clicking on?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Dynamic Pictureboxs

    as you add the pictureboxes, add their index in the arraylist to their .tag property

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Dynamic Pictureboxs

    for movable / resizable runtime controls have a look at the link in my signature

  6. #6

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: Dynamic Pictureboxs

    ok so ive tried using this now, and im still running into the same problem. when i click the picturebox, how do i check what tag it is?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Dynamic Pictureboxs

    vb Code:
    1. Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    2.  
    3.      Dim MyPb As PictureBox = CType(sender, PictureBox)
    4.  
    5.      msgbox(MyPb.tag.tostring)
    6. End Sub

  8. #8

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: Dynamic Pictureboxs

    hmm somethings wrong. i can create new pictureboxes with this:

    Code:
    Private Sub Label1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.TextChanged
            If Label1.Text = "pic1" Then
                Dim wall As New PictureBox
                wall.Location = MousePosition
                wall.BackColor = Color.Silver
                wall.Size = New Point(104, 16)
                If Form3.OpenFileDialog2.FileName = Nothing Then
                    If Form3.ColorDialog2.Color = Nothing Then GoTo a
                    wall.BackColor = Form3.ColorDialog2.Color
    a:
                Else
                    Dim img As Image = Image.FromFile(Form3.OpenFileDialog2.FileName)
                    wall.Image = img
                End If
                walls.Add(wall)
                For Each PictureBox In walls
                    Dim q As Integer = -1
                    q = q + 1
                    i = q
                Next
                drag = True
                wall.Tag = i.ToString
                Me.Controls.Add(wall)
                AddHandler wall.MouseDown, AddressOf wall_mousedown
            End If
        End Sub
    and then i have the mouse_down sub thats for dynamic controls:

    Code:
    Dim wstr As String
        Private Sub wall_mousedown(ByVal sender As Object, ByVal e As MouseEventArgs)
            Dim wall As PictureBox = CType(sender, PictureBox)
            wstr = wall.Tag
            MsgBox(wstr)
            If drag = True Then
                drag = False
                Label1.Text = ""
            Else
                drag = True
            End If
        End Sub
    End Class
    and finally the mouse_move sub

    Code:
    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            If wstr = Nothing = False Then
                If drag = True Then
                    Dim g As Integer = Val(wstr)
                    walls(g).Location = MousePosition
                End If
    
            End If
    
        End Sub
    when i create two new picboxs, it still wants to move the first one when i add the second one.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Dynamic Pictureboxs

    Code:
    For Each PictureBox In walls
                    Dim q As Integer = -1
                    q = q + 1
                    i = q
                Next
                drag = True
                wall.Tag = i.ToString
    i is always 0

  10. #10

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: Dynamic Pictureboxs

    oooh haha gotts dim g before loop... thanks

  11. #11
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    Re: Dynamic Pictureboxs

    It looks like your "wstr" variable is not global, so I don't see how you're expecting the get the correct value from that. Also, instead of using Form_MouseMove, create a new event for your picturebox control instead: wall_MouseMove, and add your code to that. Then the arguments from wall_MouseMove will tell you which picturebox has been clicked.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Dynamic Pictureboxs

    i'll let you take over on this 1 virtueone

  13. #13

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: Dynamic Pictureboxs

    good idea. thanks lemme try these ideas out

  14. #14
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    Re: Dynamic Pictureboxs

    Quote Originally Posted by .paul. View Post
    i'll let you take over on this 1 virtueone
    I was just testing out something very similar today while learning to build dynamic elements and such. I'm sure your responses would be much more educated paul.

  15. #15

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: Dynamic Pictureboxs

    ok heres my code now;

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Form3.Show()
    5.     End Sub
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         Form3.Show()
    9.         Button1.Visible = False
    10.     End Sub
    11.     Dim walls As New ArrayList
    12.     Dim drag As Boolean = False
    13.     Dim i As Integer = -1
    14.     Dim i2 As Integer
    15.     Private Sub Label1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.TextChanged
    16.         If Label1.Text = "pic1" Then
    17.             Dim wall As New PictureBox
    18.             wall.Location = MousePosition
    19.             wall.BackColor = Color.Silver
    20.             wall.Size = New Point(104, 16)
    21.             If Form3.OpenFileDialog2.FileName = Nothing Then
    22.                 If Form3.ColorDialog2.Color = Nothing Then GoTo a
    23.                 wall.BackColor = Form3.ColorDialog2.Color
    24. a:
    25.             Else
    26.                 Dim img As Image = Image.FromFile(Form3.OpenFileDialog2.FileName)
    27.                 wall.Image = img
    28.             End If
    29.             walls.Add(wall)
    30.             For Each PictureBox In walls
    31.                 q1 = q1 + 1
    32.                 i = q1
    33.             Next
    34.             drag = True
    35.             wall.Tag = i.ToString
    36.             Me.Controls.Add(wall)
    37.             AddHandler wall.MouseDown, AddressOf wall_mousedown
    38.         End If
    39.     End Sub
    40.     Dim q1 As Integer = -1
    41.     Private Sub wall_mousedown(ByVal sender As Object, ByVal e As MouseEventArgs)
    42.         Dim wall As PictureBox = CType(sender, PictureBox)
    43.         AddHandler wall.MouseMove, AddressOf wall_mousemove
    44.         If drag = True Then
    45.             drag = False
    46.             Label1.Text = ""
    47.         Else
    48.             drag = True
    49.  
    50.         End If
    51.     End Sub
    52.     Private Sub wall_mousemove(ByVal sender As Object, ByVal e As MouseEventArgs)
    53.         If drag = True Then
    54.             Dim wall As PictureBox = CType(sender, PictureBox)
    55.             wall.Location = New Point(MousePosition.X - 20, MousePosition.Y - 10)
    56.         End If
    57.     End Sub
    58. End Class

    the problems are that when i move the mouse too quickly, it goes outside the picturebox, and the wall_mousemove event stops firing off. Also, it does not distinguish what wall to move, so it moves whatever the mouse is over.

  16. #16
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    Re: Dynamic Pictureboxs

    The MouseMove event should only affect the controls that it is attached to, so I don't know why it would be moving other things.... Make sure the "sender" value matches the control that you want to move, if not, then obviously it wont move the correct control.

    Try attaching the mousemove event along with the mousedown event:
    AddHandler wall.MouseDown, AddressOf wall_mousedown
    AddHandler wall.MouseMove, AddressOf wall_mousemove

    Also, try using another method for moving the item, to avoid the out-of-bounds mouse over problem. Reference this recent post for help with that: http://www.vbforums.com/showthread.php?t=608131

    Does this help you at all?

  17. #17

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Re: Dynamic Pictureboxs

    hmm... a fix for my problems would be somehow clipping the mouseposition when drag = true, to the wall bounds. now, i need to be able to get the location and size of the wall i click on, then use something like a timer to check if the mouseposition is intersecting with the wall.

    EDIT:
    alright i fixed it. now i need help with one last problem. When im dragging a picturebox, and drag it over another picturebox, the one that im dragging swaps to the one that i mouse over. any way around this would help!
    Last edited by manbearpig001; Mar 21st, 2010 at 09:35 PM.

  18. #18
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    Re: Dynamic Pictureboxs

    Hey manbear, do you mind posting your updated code? For my own benefit, I'd like to know how you're handling the "snapping" to the wall bounds, but it would help me to help you resolve the last problem also. There is probably a couple simple ways to fix your problem...

    Set a global variable for the control that is clicked.
    So, on your mousedown event, add something like:

    itemSelected = CType(sender, PictureBox)

    Then on your mousemove event, move your control based of the "itemSelected" value, instead of relying on the "sender" value.

    You could also just make the current selected control the front-most item, so that the mousemove should only affect that item (I think):

    wall.BringToFront()

    Hope this helps you!

  19. #19
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    Re: Dynamic Pictureboxs

    By the way, this is the program I made recently learning to build and move custom controls....

    Code:
    Dim Moveable As Boolean
    Dim LastPos As Point
    
    Dim newItemID As Integer
    Dim itemSelect As Integer
    
    Dim pbArray As New ArrayList
    
        Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
    
            newItemID = newItemID + 1
    
            Dim pb As PictureBox
            pb = New PictureBox
            pb.Name = newItemID
    
            pb.Location = Me.PointToClient(Cursor.Position)
            pb.Tag = newItemID
            pb.BorderStyle = 2
            AddHandler pb.MouseDown, AddressOf ClickPictureMouseDown
            AddHandler pb.MouseUp, AddressOf ClickPictureMouseUp
            AddHandler pb.MouseMove, AddressOf ClickPictureMouseMove
            Controls.Add(pb)
    
        End Sub
    
        Private Sub ClickPictureMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
            itemSelect = DirectCast(sender, PictureBox).Tag.ToString
    
            Moveable = True
            Dim pos As Point = Cursor.Position
            pos.Offset(-DirectCast(sender, PictureBox).Location.X, -DirectCast(sender, PictureBox).Location.Y)
            LastPos = pos
            DirectCast(sender, PictureBox).BringToFront()
    
            If e.Button = MouseButtons.Right Then
                Controls.Remove(DirectCast(sender, PictureBox))
            End If
        End Sub
    
        Private Sub ClickPictureMouseUp(ByVal sender As Object, ByVal e As EventArgs)
            Moveable = False
        End Sub
    
        Private Sub ClickPictureMouseMove(ByVal sender As Object, ByVal e As EventArgs)
            If Moveable Then
                Dim pos As Point = Cursor.Position
                pos.Offset(-LastPos.X, -LastPos.Y)
                DirectCast(sender, PictureBox).Location = pos
            End If
        End Sub
    This creates a new picturebox each time the user clicks on the form.
    It allows the user to click on any added picturebox and darg it around.
    Seems so simple, but I was quite satisfied when it worked!

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