Click here to Skip to main content
16,022,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code will create the pictureboxes in the form ,if they intersect msgbox will appear. i tried in timer event but its not working.



VB
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
Posted
Comments
Sandeep Mewara 22-Jun-12 15:13pm    
Elaborate 'not working'.
Maciej Los 22-Jun-12 17:08pm    
Agree ;)
Dave Kreskowiak 22-Jun-12 20:46pm    
We have no idea what you mean by "not working" since we have no idea what this code is expected to.

Also, there's no timer in this code so there's a major problem nor do we know what you're supposed to be doing with the timer.

Method and variable names like "q", "gg" and "qq" are absolute garbage. They don't describe what those items do at all.
Sergey Alexandrovich Kryukov 22-Jun-12 22:06pm    
"Not working" is not informative. Why doing such strange thing with picture boxes?
"if qq(a) = True" is ridiculous, shows a person who has a very vague idea on how to program, must be:
"if SomeBooleanFunction(someArgument)..."
result of Boolean function is already Boolean, comparing it with True gives the same result.
--SA

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