Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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().


VB
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
Posted

1 solution

I noticed this is a repeat from http://www.codeproject.com/Questions/403349/avoid-overlapping-of-pictureboxes

You're saying you can't use an array with IntersectsWith, so why not just do a "For Each ... Next" loop through your array, calling IntersectsWith
 
Share this answer
 

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