Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDrag items from one position in a list to another displaying textual icons
Keywordslist, drag, drag and drop, text icon
CategoriesControls
 
Use drag and drop. When the user clicks on the list or on a label listing the list items, the list's MouseDown event handler calls subroutine PrepareDragIcon to prepare an appropriate drag icon. It then starts the drag.
 
Private Sub List1_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    ' Prepare the drag icon.
    PrepareDragIcon List1, List1.List(List1.ListIndex)

    ' Start the drag.
    List1.Drag
End Sub

' Set this control's DragIcon property.
Private Sub Label1_MouseDown(Index As Integer, Button As _
    Integer, Shift As Integer, X As Single, Y As Single)
    ' Prepare the drag icon.
    PrepareDragIcon Label1(Index), Label1(Index).Caption

    ' Start the drag.
    Label1(Index).Drag
End Sub
 
Subroutine PrepareDragIcon draws the item's text into a PictureBox. For icons, the picture should be 32 by 32 pixels. It copies the picture into an ImageList control and then uses the ImageList's ListImages collection's ExtractIcon method to get an icon. It sets the dragging control's DragIcon to this.
 
' Set this control's DragIcon property.
Private Sub PrepareDragIcon(ByVal ctl As Control, ByVal txt _
    As String)
    ' Write the text into the PictureBox.
    picIcon.Font.Name = ctl.Font.Name
    picIcon.Font.Size = ctl.Font.Size
    picIcon.Font.Bold = ctl.Font.Bold
    picIcon.Line (0, 0)-(picIcon.ScaleWidth, _
        picIcon.ScaleHeight), picIcon.BackColor, BF

    picIcon.CurrentX = (picIcon.ScaleWidth - _
        picIcon.TextWidth(txt)) / 2
    picIcon.CurrentY = (picIcon.ScaleHeight - _
        picIcon.TextHeight(txt)) / 2
    picIcon.Print txt
    picIcon.Picture = picIcon.Image

    imlIcon.ListImages.Clear
    imlIcon.ListImages.Add , , picIcon.Picture

    ctl.DragIcon = imlIcon.ListImages(1).ExtractIcon
End Sub
 
If the user drops the item on the list, the List1_DragDrop event handler verifies that the drop source is also the list control (it only lets you drag from the list to the list). It determines where the item should go and moves it there.
 
Private Sub List1_DragDrop(Source As Control, X As Single, _
    Y As Single)
Dim dropped_value As String
Dim new_position As Integer

    If Not (Source Is List1) Then Exit Sub

    ' Get the item's drop index.
    new_position = GetListItemIndex(List1, X, Y)

    ' Add the item in this position.
    If new_position > List1.ListCount Then
        ' Add at the end.
        List1.AddItem List1.List(List1.ListIndex)
    Else
        ' Add at this position.
        List1.AddItem List1.List(List1.ListIndex), _
            new_position
    End If

    ' Remove the item from its old position.
    If List1.ListIndex >= 0 Then List1.RemoveItem _
        List1.ListIndex
End Sub
 
If the user drops the item on a label, the Label1_DragDrop event handler verifies that the drop source is also a label (it only lets you drag from a label to a label). It determines the labels the item is being dragged to and from and switches the values in those labels.
 
Private Sub Label1_DragDrop(Index As Integer, Source As _
    Control, X As Single, Y As Single)
Dim tmp As String

    If Not (TypeOf Source Is Label) Then Exit Sub

    tmp = Label1(Index).Caption
    Label1(Index).Caption = Source.Caption
    Source.Caption = tmp
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated