Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Drag Multiple Controls with Shadows Effects

0.00/5 (No votes)
27 Apr 2017 1  
This tip shows how to make a multiple controls drag effect in winform.

Introduction

Working with the winforms native drag&drop, I happened to have to give the user the perception of what's dragging. Looking for a variety of articles, I came across various drag helpers, many of which use retrieved to specific Windows API, as illustrated, for example, in this article.

The main problem was to create multiple image files.

Background

To create this effect, I used a transparent form to draw the controls to be dragged, absolutely positioned respect to the area of the form itself. For multiple selection, I use the standard Mouse shell selection, or click on control to single Drag.

The drawExtension module in the project, is only for border effects on control, but is not binding on the project itself. For controls drawing, refer to the namespace Graphics methods.

Using the Code

If buttonOnControlIsPressed Then
'Preparing drag form....
    buttonOnControlIsPressed = False
    Dim splacement As Integer = 5
    'Give 5px splacement: The first available point out of the bound of the referral control
    initialSplacement = New Point(e.X + splacement, e.Y + splacement)
    frmD = New frmDrag With {.Location = Me.PointToScreen(initialSplacement), _
           .Size = Me.ClientSize, .Visible = False, .TopMost = True}
    frmDLocationBeforeDrag = frmD.Location
    controlsToDrag = New List(Of Control)
    For Each c As Control In Me.Controls
        If c.Tag("selected") = True Then controlsToDrag.Add(c)
    Next
    For Each c As Control In controlsToDrag
        Dim img As Image = getImageFromControl(c)
        frmD.Controls.Add(New PictureBox With _
        {.Size = img.Size, .Image = img, .Location = New Point(c.Location.X, c.Location.Y)})
    Next
    startPointBeforeControlDrag = Me.PointToClient(MousePosition)
    If Me.DoDragDrop(Me, DragDropEffects.All) = DragDropEffects.None Then
        controlsToDrag.ForEach(Sub(x)
        x.Tag("selected") = False
        x.DrawBorder()
        End Sub)
        frmD.Close()
    End If
End If

Building of DragForm is done when drag starts on any controls in form. All necessary events are initialized to the added control event in Main Form, so you can perform the same effect on runtime added controls

Private Sub frmMain_ControlAdded(sender As Object, e As ControlEventArgs) Handles Me.ControlAdded
    'Set this handles/property here, to make draggable controls added at runtime also
    'Adding some custom property in tag of control
    Dim htCustomProperty As New Hashtable
    htCustomProperty("selected") = False
    e.Control.Tag = htCustomProperty
    'Adding custom handlers for all controls in form
    AddHandler e.Control.MouseDown, AddressOf _controlsMouseDown
    AddHandler e.Control.MouseUp, AddressOf _controlsMouseUp
    AddHandler e.Control.MouseMove, AddressOf _controlMouseMove
    e.Control.Cursor = Cursors.SizeAll
    allControls.Add(e.Control)
End Sub

Points of Interest

The technique of a transparent and opaque additional form is also useful to solve the problem to create form with shadow.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here