Dragand Drop Functionality

Drag-and-drop capability is an important feature of most modern form-based applications. There are a number of important differences in drag-and-drop functionality between Visual Basic 6.0 and Visual Basic .NET. This section provides an overview of the differences so that you can better understand how they will affect your application.

Drag-and-Drop Functionality in Visual Basic 6.0

Visual Basic 6.0 supports two kinds of drag-and-drop operations: standard drag-and-drop functionality, which supports moving items between controls within a single form, and OLE drag-and-drop functionality, which supports moving items between applications. This section focuses on OLE drag-and-drop functionality.

The standard Visual Basic 6.0 controls have varying degrees of support for OLE drag-and-drop functionality.

Table 9.2 lists the standard Visual Basic 6.0 controls and their support for manual and automatic drag-and-drop operations.

Table 9.2: Visual Basic 6.0 Controls: Support for OLE Drag-and-Drop Functionality Controls OLEDragMode OLEDropMode

TextBox, PictureBox, Image, RichTextBox, MaskedBox

VbManual, vbAutomatic

vbNone, vbManual, vbAutomatic

ComboBox, ListBox, DirListBox, FileListBox, DBCombo, DBList, TreeView, ListView, ImageCombo, DataList, DataCombo

VbManual, vbAutomatic

vbNone, vbManual

Form, Label, Frame, CommandButton, DriveListBox, Data, MSFlexGrid, SSTab, TabStrip, Toolbar, StatusBar, ProgressBar, Slider, Animation, UpDown, MonthView, DateTimePicker, CoolBar

Not supported

vbNone, vbManual

Figure 9.1 on the next page outlines the life cycle of an OLE drag-and-drop operation in Visual Basic 6.0.

Although drag-and-drop functionality is supported in Visual Basic .NET, there are differences. The next section details drag-and-drop functionality in Visual Basic .NET.

The data is requested of the source control The source control can optionally complete if it was not specified in the OLEStartDrag the operation by deleting source data. event method. Figure 9.1

The life cycle of a Visual Basic 6.0 drag-and-drop operation

The data is requested of the source control The source control can optionally complete if it was not specified in the OLEStartDrag the operation by deleting source data. event method. Figure 9.1

The life cycle of a Visual Basic 6.0 drag-and-drop operation

Drag-and-Drop Functionality in Visual Basic .NET

In Visual Basic .NET, the drag-and-drop operations are consolidated into a single framework. Drag-and-drop operations between controls is handled the same as drag-and-drop operations between applications. This change simplifies the programming burden for new development, but it requires rewriting drag-and-drop code for existing applications.

Figure 9.2 outlines the life cycle of a drag-and-drop procedure in Visual Basic .NET. Note that this cycle applies to both control-to-control and application-to-application drag-and-drop operations.

Figure 9.2

Life cycle of a Visual Basic .NET drag-and-drop operation

Figure 9.2

Life cycle of a Visual Basic .NET drag-and-drop operation

The changes necessary to make the drag-and-drop code work in Visual Basic .NET are fairly significant. Because of changes in the drag-and-drop programming model, the upgrade wizard cannot automatically make the modifications. Instead, the methods are unchanged, and you have to implement the logic in the Visual Basic .NET drag-and-drop event model. The following code example shows how drag-and-drop operations are handled in Visual Basic 6.0.

Private Sub MyPictureBox_MouseDown(Button As Integer, Shift As Integer, _ X As Single, Y As Single) MyPictureBox.OLEDrag End Sub

Private Sub MyPictureBox_OLEDragDrop(Data As DataObject, Effect As Long, _ Button As Integer, Shift As Integer, X As Single, Y As Single)

If Data.GetFormat(vbCFFiles) Then Dim i As Integer For i = 1 To Data.Files.Count Dim ImagePath As String Dim Index As Integer

ImagePath = Data.Files(i) Index = ImageIndexOf(ImagePath) If Index = -1 Then

Select Case UCase(Right(ImagePath, 4)) Case ".BMP", ".JPG", ".GIF"

ImageList.AddItem ImagePath

ImageList.ListIndex = ImageList.ListCount - 1

Set MyPictureBox.Picture = LoadPicture(ImagePatli) End Select

Else

ImageList.ListIndex = Index End If

Next End If

End Sub

Private Sub MyPictureBox_OLEStartDrag(Data As DataObject, AllowedEffects As Long) Data.Files.Clear Data.Files.Add ImageList Data.SetData , vbCFFiles AllowedEffects = vbDropEffectCopy End Sub

Private Function ImageIndexOf(ImagePath As String) As Integer Dim i As Integer

For i = 0 To ImageList.ListCount

If ImageList.List(i) = ImagePath Then ImageIndexOf = i Exit Function End If

Next

ImageIndexOf = -1 End Function

Instead of upgrading the Visual Basic 6.0 code, it is necessary to replace it with the Visual Basic .NET drag-and-drop event model. In the Visual Basic .NET event model, information is passed back and forth in a different way. The following code example shows how to implement the equivalent drag-and-drop operation demonstrated in the preceding Visual Basic .NET.

Private Sub MyForm_Load(ByVal eventSender As System.Object, _

ByVal eventArgs As System.EventArgs) Handles MyBase.Load MyPictureBox.AllowDrop = True Application.DoEvents() End Sub

Private Sub MyPictureBox_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles MyPictureBox.DragDrop ' Clear out the existing image and ensure that any resources ' are released.

If Not MyPictureBox.Image Is Nothing Then MyPictureBox.Image.Dispose() MyPictureBox.Image = Nothing End If

Dim Images() As String = e.Data.GetData(DataFormats.FileDrop, True)

Dim i As Integer Dim Index As Integer

Index = ImageIndexOf(Images(i)) If Index = -1 Then

ImageList.SelectedIndex = ImageList.Items.Add(Images(i))

Else

ImageList.SelectedIndex = Index End If

Next

' Set the picture to the last image that was added. MyPictureBox.Image = Image.FromFile(ImageList.SelectedItem) End Sub

Private Sub PictureDisplay_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles MyPictureBox.DragEnter If e.Data.GetDataPresent(DataFormats.FileDrop) Then Dim FileType As String

Dim file() As String = e.Data.GetData(DataFormats.FileDrop, True)

FileType = UCase(Microsoft.VisualBasic.Right(file(0), 4)) Select Case FileType

Case ".BMP", ".JPG", ".GIF"

e.Effect = DragDropEffects.Copy Case Else e.Effect = DragDropEffects.None

End Select

Else e.Effect = DragDropEffects.None End If End Sub

Private Sub PictureDisplay_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles MyPictureBox.MouseDown Dim file(0) As String file(0) = ImageList.SelectedItem

Dim d As New DataObject(DataFormats.FileDrop, file) ImageList.DoDragDrop(d, DragDropEffects.Copy) End Sub

Private Function ImageIndexOf(ByRef ImagePath As String) As Short Dim i As Short

For i = 0 To ImageList.Items.Count

If VB6.GetItemString(ImageList, i) = ImagePath Then ImageIndexOf = i Exit Function End If

Next

ImageIndexOf = -1 End Function

This example should give you an idea of the kind of work you have to do to replace drag-and-drop functionality in Visual Basic .NET.

+1 0

Post a comment