Click here to Skip to main content
15,895,606 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Drag Multiple Controls with Shadows Effects

Rate me:
Please Sign up or sign in to vote.
3.86/5 (7 votes)
27 Apr 2017CPOL1 min read 9.7K   165   4   2
This tip shows how to make a multiple controls drag effect in winform.

Image 1

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

VB
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

VB
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Collaborative Group (No members)


Comments and Discussions

 
Questiongood job! Pin
Member 1124181029-Apr-17 10:53
Member 1124181029-Apr-17 10:53 
GeneralGreat work! Pin
skimmenthal28-Apr-17 1:14
skimmenthal28-Apr-17 1:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.