Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making an application in which number of pictureboxes created at runtime but they do not overlap to each other
I wrote a code bt its only for two pictureboxes.How can i do that for number of pictureboxes..



code:

VB
Public Class Form1
    Dim pic1, pic2 As Rectangle
    Dim drag, iscollided As Boolean
    Dim mousex, mousey As Integer

    Private Sub pic_Mousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If drag Then
            Dim mposition As New Point()
            mposition = Me.PointToClient(MousePosition)
            mposition.Offset(mousex, mousey)
            sender.location = mposition
        End If
    End Sub

    Private Sub pic_Mouseup(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        drag = False
        Windows.Forms.Cursor.Clip = Nothing
        Me.Cursor = Cursors.Arrow
        sender.invalidate()
    End Sub

    Private Sub pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Me.Cursor = Cursors.Hand
            drag = True
            mousex = -e.X
            mousey = -e.Y
            Dim left As Integer = Me.PointToClient(MousePosition).X - sender.location.x
            Dim right As Integer = Me.PointToClient(MousePosition).Y - sender.location.y
            Dim width As Integer = Me.ClientSize.Width - (sender.width - left)
            Dim height As Integer = Me.ClientSize.Height - (sender.width - right)
            Windows.Forms.Cursor.Clip = Me.RectangleToScreen(New Rectangle(left, right, width, height))
            sender.invalidate()
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        pic1.Location = PictureBox1.Location
        pic2.Location = PictureBox2.Location
        pic1.Size = PictureBox1.Size
        pic2.Size = PictureBox2.Size
        PictureBox1.SendToBack()
        PictureBox2.SendToBack()
        iscollided = CheckCollision()
        If iscollided = True Then
            PictureBox1.BackColor = Color.Transparent
        Else
            PictureBox1.BackColor = Color.Black
        End If
        Me.Text = iscollided.ToString
        AddHandler PictureBox1.MouseDown, AddressOf pic_MouseDown
        AddHandler PictureBox1.MouseMove, AddressOf pic_Mousemove
        AddHandler PictureBox1.MouseUp, AddressOf pic_Mouseup
        AddHandler PictureBox2.MouseDown, AddressOf pic_MouseDown
        AddHandler PictureBox2.MouseMove, AddressOf pic_Mousemove
        AddHandler PictureBox2.MouseUp, AddressOf pic_Mouseup

    End Sub


    Public Function CheckCollision()
        Dim result As Boolean = False
        If pic1.IntersectsWith(pic2) Then
            result = True
        Else
            result = False
        End If
        Return result
    End Function
End Class
Posted
Updated 13-Jun-12 13:08pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Jun-12 18:29pm    
It's very likely that you abuse such a simple thing as a PictureBox. With more complex tasks, it's better to avoid using them. So, what are you trying to achieve? What is the goal of your application and what are you trying to use picture boxes for?
--SA
boys13 14-Jun-12 2:03am    
i want to drag picture from treeview and in the form picture will create.
when a picturebox move over other picturebox ,it will resist or diable.any two pictureboxes should not stay on each other.

1 solution

You should change CheckCollision() in such a way that control "pic1" will always be your control you are moving and for the other one you can use GetChilAtPoint[^] method of form controls. If the method returns a control of type "PictureBox" then you should cast the control in picturebox and use "IntersectsWith" method as you already did.
 
Share this answer
 
Comments
boys13 22-Jun-12 2:13am    
This is worked but how can i do that in array of picturebox. i tried it with function with for each loop but it didnt work. how can i use that in timer.?
Shahan Ayyub 23-Jun-12 4:20am    
can we get more details (piece of code) about how you are dealing with the case ?
_Amy 31-Jul-12 2:21am    
My +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900