View Single Post
Old 05-16-2016, 02:07 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,031

What is the purpose of the color swapping?
Does it have to be a picturebox?
If I just needed colored rectangles that I could drag on and swap the colors I would just draw them using FillRectangle calls, and use the intersect method of the rectangle to detect in which rectangle a mouse down and a mouse up occurred.

If you feel you want to use controls, does it have to be a picturebox?
You can change the background color of a label (set autosize to false so you can make the control the same size as your picturebox and use them in place of the picturebox) or the background color of a panel.

An advantage to using a label or panel control is that they support Drag and Drop.
A picturebox doesn't support Drag and Drop.

As an example, I put six panels on a form, setting three of the backgrounds red, and three of them blue.
I enabled the AutoDrop property on all six controls.
I tried passing the panel as the drag object but couldn't cast it correctly on the drop side, so gave up trying as I don't have the time to figure out if it could be done.
I just copy the reference to a form scope level variable instead.
For the example I just use the same event handlers for all six panels.
Public Class Form1
  Dim p1 As Panel
  Dim p2 As Panel
  Private Sub Panel_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Panel6.MouseDown,
                  Panel5.MouseDown, Panel4.MouseDown, Panel3.MouseDown, Panel2.MouseDown, Panel1.MouseDown
    p1 = DirectCast(sender, Panel)
    p1.DoDragDrop("dummy", DragDropEffects.Copy)
  End Sub

  Private Sub Panel_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Panel6.DragEnter,
                  Panel5.DragEnter, Panel4.DragEnter, Panel3.DragEnter, Panel2.DragEnter, Panel1.DragEnter
    p2 = DirectCast(sender, Panel)
    e.Effect = DragDropEffects.Copy
    Debug.Print("Swaping {0}.backcolor with {1}.backcolor", p1.Name, p2.Name)
  End Sub

  Private Sub Panel_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Panel6.DragDrop,
                  Panel5.DragDrop, Panel4.DragDrop, Panel3.DragDrop, Panel2.DragDrop, Panel1.DragDrop
    Dim c As Color = p1.BackColor
    p1.BackColor = p2.BackColor
    p2.BackColor = c
  End Sub
End Class
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote