5 Replies - 682 Views - Last Post: 22 June 2012 - 12:26 PM Rate Topic: -----

#1 Roseholmes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 12

collision of picturebox array

Posted 18 June 2012 - 01:11 AM

In my code i used a button to create a picturebox & it can move. I want to avoid overlapping of any pictureboxes created in the form.i used timer for it but i am having a trouble to avoid overlapping & i cant apply array in the intersectwith().


Public Class Form1
    Dim pic As New List(Of PictureBox)
    Dim mousex, mousey As Integer
    Dim drag As Boolean = False
    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 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 Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If pic(1).Bounds.IntersectsWith(pic(2).Bounds) Then
            Button1.Enabled = False
        Else
            pic(1).Enabled = True
        End If
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New PictureBox
        a.Image = Image.FromFile("C:\Users\Rose\Desktop\pic.jpeg")
        Dim pic = a
        Me.Controls.Add(pic)
        AddHandler pic.MouseDown, AddressOf pic_MouseDown
        AddHandler pic.MouseMove, AddressOf pic_Mousemove
        AddHandler pic.MouseUp, AddressOf pic_Mouseup
    End Sub
End Class





Is This A Good Question/Topic? 0
  • +

Replies To: collision of picturebox array

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: collision of picturebox array

Posted 18 June 2012 - 06:20 AM

Pass the sender object to this function after you cast it back to a PB.

'cast your objects
CType(sender, PictureBox)


Private Function IntersectedWith(pb As PictureBox) As Boolean
  For Each _pb As PictureBox In pics
    If Not _pb Is pb Then
     If _pb.IntersectsWith(pb) Then
       Return True
     Else 
       Continue For
    End If
  Next
  Return False
End Function 

Was This Post Helpful? 1
  • +
  • -

#3 Roseholmes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 12

Re: collision of picturebox array

Posted 22 June 2012 - 12:41 AM

collision is worked. In button when two pictureboxes intersect, msgbox appear once.That's why i tried in timer but its not working.




Public Class Form1
    Private pic As New ArrayList()
    Dim drag As Boolean = False
    Dim mousex, mousey As Integer


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.Click

        Dim a As New PictureBox

        a.Location = New System.Drawing.Point(50, 50)

        a.BackColor = Color.Khaki
        Me.Controls.Add(a)
        pic.Add(a)
        AddHandler a.MouseDown, AddressOf pic_MouseDown
        AddHandler a.MouseMove, AddressOf pic_Mousemove
        AddHandler a.MouseUp, AddressOf pic_Mouseup
        AddHandler a.MouseClick, AddressOf gg
        If qq(a) = True Then
            Debug.Print("collision occured.")
        Else
            Debug.Print("no intersectin")
        End If
    End Sub

    Public Sub gg(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim msg As MsgBoxResult = MsgBox("Do you want to remove?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "warning")
            If msg = MsgBoxResult.Yes Then
                Me.Controls.Remove(sender)
            End If
        End If
    End Sub

    Public Function qq(ByVal pb As PictureBox) As Boolean
        For Each q As PictureBox In pic
            If Not q Is pb Then
                If q.Bounds.IntersectsWith(pb.Bounds) Then
                    Return True
                Else
                    Continue For
                End If
            End If
        Next
        Return False
    End Function
    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 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

End Class





Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: collision of picturebox array

Posted 22 June 2012 - 07:06 AM

Ok, tell us more about what your wanting and where it's not working.

Best practice for runtime controls where you create delegates you need to remove them. When use use Addhandler remember you should RemoveHandler - it's the same format. I know it's easy to just write sender.<something> but it's not the best practice and if you where running Option Strict On the compiler would not let you get away with that. It is just a recommendation.

'do this once at the beginning of the sub and reuse it throughout the sub 
Dim pb As PictureBox = CType(sender, PictureBox)

Was This Post Helpful? 0
  • +
  • -

#5 Roseholmes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 12

Re: collision of picturebox array

Posted 22 June 2012 - 11:56 AM

Actually i am making a project in which we can drag an specific node(treeview) and speifc pictureboxes generate in form but without overlapping. uptil now using function intersecting logic is worked only if we press the "button1" twice.By moving pictureboxes & if they collide nothing happen. Thats why i want to use timer so that any two pictureboxes collide at any time & perticular picturebox diable or something.

Its about electronic field.so when we drag an "AND GATE" from treeview & in the form AND-block(picturebox) will be created.when two Blocks are collide blocks have to disable or something and Blocks are connected to wire(lineshape).,wire can be connected to other block.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 956
  • View blog
  • Posts: 3,676
  • Joined: 02-July 08

Re: collision of picturebox array

Posted 22 June 2012 - 12:26 PM

Your check for collision needs to be in the pic_Mousemove event. For this I would use a New Rectangle based on the current position then move that Rectangle then check for collision if not give the changes for the position to the PB.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1