Drag and Drop

Drag and Drop

Drag-and-drop capability is an important function of most modern applications. There are a number of important differences between Visual Basic 6 and Visual Basic .NET with respect to drag and drop. To better understand how these changes will affect your application, let s take a quick overview of this feature.

Drag and Drop in Visual Basic 6

Visual Basic 6 supported two kinds of drag-and-drop operations. Standard drag and drop is intended to support drag and drop between controls within a single form. OLE drag and drop was designed to support drag-and-drop capability between applications and is the focus of this section.

The standard Visual Basic 6 controls have varying degrees of support for OLE drag-and-drop operations. Table 10-2 lists the standard Visual Basic 6 controls and their support for manual and automatic drag-and-drop operations.

Table 10-2 Visual Basic 6 Controls Grouped According to Their Support for OLE Drag and Drop

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

For the sake of brevity, this section deals exclusively with a manual drag-and-drop application. To better illustrate how the upgrade process affects OLE drag-and-drop operations, we have provided a sample application named OLEDragAndDrop on the companion CD. It permits the user to drag and drop image files to and from the application and implements OLE drag and drop in full manual mode. This sample should give you a better idea of exactly what changes you will need to make in your own application. Figure 10-3 shows an image from the sample OLEDragAndDrop application.

Figure 10-3

OLEDragAndDrop application.

Figure 10-4 outlines the life cycle of an OLE drag-and-drop operation in Visual Basic 6. The next section moves on to how things are done in Visual Basic .NET.

Figure 10-4

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

Drag and Drop in Visual Basic .NET

In Visual Basic .NET, the drag-and-drop operations have been consolidated into a single framework. Drag and drop between controls is handled in exactly the same manner as drag and drop between applications. This change simplifies the programming burden for new development but requires the rewriting of drag-and-drop code for existing applications. Figure 10-5 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.

Figure 10-5

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

The OLEDragAndDrop sample application contains the code necessary to implement the drag-and-drop operation in Visual Basic 6. The following is an excerpt from the Form1.frm file in that project:

Private Sub PictureDisplay_MouseDown(Button As Integer, _    Shift As Integer, X As Single, Y As Single)    PictureDisplay.OLEDrag End Sub Private Sub PictureDisplay_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 file As String          file = Data.Files(i)                       If Not EntryExists(file) Then             Select Case UCase(Right(file, 4))                Case ".tif", ".jpg", ".gif"                   FileList.AddItem file                   FileList.ListIndex = FileList.ListCount - 1                                            Set PictureDisplay.Picture = LoadPicture(file)             End Select          Else             SelectListItem file          End If       Next    End If End Sub Private Sub PictureDisplay_OLEStartDrag(Data As DataObject, _    AllowedEffects As Long)    Data.Files.Clear         Data.Files.Add FileList    Data.SetData , vbCFFiles    AllowedEffects = vbDropEffectCopy End Sub

The changes necessary to make the drag-and-drop code work in Visual Basic .NET are fairly significant. Due to the changes in the drag-and-drop programming model, the Upgrade Wizard cannot automatically make the modifications for you. It simply leaves the methods alone, leaving it to you to implement the logic in the Visual Basic .NET drag-and-drop event model.

Instead of upgrading the Visual Basic 6 code, we have provided a new Visual Basic .NET implementation of the same features. This helps demonstrate how the drag-and-drop event model has changed and how to properly pass information back and forth. The following code shows how to implement the equivalent drag-and-drop operation from the OLEDragAndDrop sample application in Visual Basic .NET. (This code is also included on the companion CD.)

Private Sub Form1_Load(ByVal sender As Object, _    ByVal e As System.EventArgs) Handles MyBase.Load    PictureDisplay.AllowDrop = True End Sub Private Sub PictureDisplay_DragDrop(ByVal sender As Object, _    ByVal e As System.Windows.Forms.DragEventArgs) _    Handles PictureDisplay.DragDrop    ' Clear out the existing image and ensure that any resources     ' are released    If Not PictureDisplay.Image Is Nothing Then       PictureDisplay.Image.Dispose()       PictureDisplay.Image = Nothing    End If    Dim files() As String = e.Data.GetData(DataFormats.FileDrop, True)    Dim i As Integer    For i = 0 To files.Length - 1       If Not EntryExists(files(i)) Then          FileList.SelectedIndex = FileList.Items.Add(files(i))       Else          SelectListItem(files(i))       End If    Next    ' Set the picture to the last image added    PictureDisplay.Image = Image.FromFile(FileList.SelectedItem) End Sub Private Sub PictureDisplay_DragEnter(ByVal sender As Object, _    ByVal e As System.Windows.Forms.DragEventArgs) _    Handles PictureDisplay.DragEnter    If e.Data.GetDataPresent(DataFormats.FileDrop) Then       Dim file() As String = e.Data.GetData(DataFormats.FileDrop, True)       Select Case UCase(Microsoft.VisualBasic.Right(file(0), 4))          Case ".tif", ".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 PictureDisplay.MouseDown    Dim file(0) As String    file(0) = FileList.SelectedItem    Dim d As New DataObject(DataFormats.FileDrop, file)    PictureDisplay.DoDragDrop(d, DragDropEffects.Copy) End Sub

Use the life-cycle diagram in Figure 10-5 to help gain an understanding of how to integrate drag and drop into your Visual Basic .NET applications. This example should be a good introduction to the issues you will encounter when dealing with drag and drop.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net