Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Interface and Graphics (http://www.xtremevbtalk.com/-net-interface-and-graphics/)
-   -   Picturebox Backcolor Drag and Drop Swap (http://www.xtremevbtalk.com/-net-interface-and-graphics/328206-picturebox-backcolor-drag-drop-swap.html)

SCylant 05-13-2016 04:11 PM

Picturebox Backcolor Drag and Drop Swap
I want to drag picturebox1.background = blue, to, picturebox2.background = green, resulting in picturebox1.background = green, and, picturebox2 = blue. I can mousedown pb1 and make pb2 blue, but pb1 also remains blue.
Mousedown to capture pb1 and color, mouse over/up pb2 only results in the pb1 mouseup event, and cannot capture pb2 and color. What is if any are my options to capture the mouseup on pb2 to capture its properties?

passel 05-16-2016 02:07 AM

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

All times are GMT -6. The time now is 04:38 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2018 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.