Drag and Drop Changes

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay  Katre, Prashant  Halari, Narayana  Rao  Surapaneni, Manu  Gupta, Meghana  Deshpande

Table of Contents
Chapter 4.   Post-migration Changes


The drag and drop mechanism has undergone lots of changes in Visual Basic .NET. All code that uses drag and drop mechanisms need to be changed in Visual Basic .NET code.

In Visual Basic 6.0, drag and drop could be accomplished by two different methods : standard drag and drop for dragging between controls on a form and OLE drag and drop for dragging between forms and applications. In Visual Basic .NET, a single model for drag and drop is used. It is similar to OLE drag and drop, but the names and parameters for drag and drop methods and events are different, and the event model is different. Drag and drop code cannot be automatically upgraded to Visual Basic .NET; it must be rewritten using the new model.

CODE EXAMPLES

Example 11

In the following Visual Basic 6.0 code example, when the user clicks on the command button, the label can be dragged from its current position to any other position on the form. The following code is in the DragDropLabel-VB folder for this chapter:

 graphics/icon01.gif Private Sub Command1_Click()     Label1.Drag  End Sub  Private Sub Form_DragDrop(Source As Control, X As_   Single, Y As Single)     Label1.Left = X     Label1.Top = Y  End Sub 

The following equivalent Visual Basic .NET code is in the DragDropLabel-VB.NET-Modified folder for this chapter:

 graphics/icon01.gif Private Sub Command1_Click(ByVal sender As _   System.Object, ByVal e As System.EventArgs) Handles_    Command1.Click     Label1.DoDragDrop(Label1, DragDropEffects.Move)  End Sub  Private Sub Form1_DragEnter(ByVal sender As Object,_   ByVal e As System.Windows.Forms.DragEventArgs) Handles_    MyBase.DragEnter     e.Effect = DragDropEffects.Move  End Sub  Private Sub Form1_DragDrop(ByVal sender As Object, _   ByVal e As System.Windows.Forms.DragEventArgs) Handles_    MyBase.DragDrop     Label1.Left = e.X     Label1.Top = e.Y  End Sub  Private Sub Label1_MouseDown(ByVal sender As Object, _   ByVal e As System.Windows.Forms.MouseEventArgs) _    Handles Label1.MouseDown     Label1.DoDragDrop(Label1, DragDropEffects.Move)  End Sub 

The DoDragDrop method is normally called in the MouseDown event of the source control. However, in this example, the DoDragMethod is called from the button click event because that is the way it was originally coded in Visual Basic 6. In the preceding code, the DoDragDrop method is also called from the Label1.MouseDown event to illustrate how the drag drop event works.

Example 12

In the following Visual Basic code example, the user can click on label1 and drag the text in label1 onto label2 , overriding the earlier text of label2 . The following code is in Drag_Drop_Example2-VB folder for this chapter:

 graphics/icon01.gif Private Sub Label1_MouseDown(Button As Integer, Shift _   As Integer, X As Single, Y As Single)     Label1.OLEDrag  End Sub  Private Sub Label1_OLEStartDrag(Data As DataObject, _   AllowedEffects As Long)     AllowedEffects = vbDropEffectCopy     Data.SetData Label1.Caption, vbCFText  End Sub  Private Sub Label2_OLEDragDrop(Data As DataObject, _   Effect As Long, Button As Integer, Shift As Integer,X _    As Single, Y As Single)     Label2.Caption = Data.GetData(vbCFText)  End Sub 

The following equivalent Visual Basic .NET code is in the Drag_Drop_Example2-VB.NET-Modified folder for this chapter:

 graphics/icon01.gif Private Sub Label1_MouseDown(ByVal sender As Object, _   ByVal e As System.Windows.Forms.MouseEventArgs) _    Handles Label1.MouseDown     Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy)  End Sub  Private Sub Label2_DragEnter(ByVal sender As Object, _   ByVal e As System.Windows.Forms.DragEventArgs) _    Handles Label2.DragEnter     e.Effect = DragDropEffects.Copy  End Sub  Private Sub Label2_DragDrop(ByVal sender As Object, _   ByVal e As System.Windows.Forms.DragEventArgs) _    Handles Label2.DragDrop     Label2.Text = e.Data.GetData(DataFormats.Text). _      ToString  End Sub 

In this code example, the contents of the label1 (i.e., text is being copied into the label 2 ) and the data format used in this drag and drop operation is of type text . It is clear that all Visual Basic 6.0 projects that contain drag and drop functionality have to be changed when these projects are upgraded to Visual Basic .NET.


Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

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