Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Enable Your Windows Forms Applications to Drag-and-Drop Data Objects : Page 8

This article demonstrates how to import files from the Windows shell and how to enhance some UI controls to make them accept input via drag-and-drop. Notable examples are the TextBox and the PictureBox controls.


advertisement
Dragging Between PictureBox Controls
Implementing drag-and-drop from within a TextBox control is more complicated than with other controls. This is because the TextBox control handles the MouseDown event on its own; subsequently, custom code must interact with that implementation. Supporting a drag operation from a PictureBox control is significantly easier.

Earlier in this article, I discussed how to drop a file name onto a PictureBox control and have it display the contained image, if any. Can you drop an in-memory bitmap too? And can you drag-and-drop images between PictureBox controls? Sure! Let's see how.

Adding drop support to a PictureBox control for bitmap objects is as easy as checking an extra condition in the DragOver handler.

If e.Data.GetDataPresent("FileNameW") Or _ e.Data.GetDataPresent(DataFormats.Bitmap) Then e.Effect = DragDropEffects.Copy Return End If

The dragged data object that contains a bitmap becomes an acceptable drop for a PictureBox control. When the drop occurs, you examine the data being carried and decide how to proceed.

If e.Data.GetDataPresent("FileNameW") Then LoadPictureFromFile(e) Return End If If e.Data.GetDataPresent(DataFormats.Bitmap) Then LoadPictureFromBitmap(e) Return End If

Dragging a picture from a PictureBox control is easy too: you can blissfully choose the MouseDown event to start the operation. In this case, there are no drawbacks or caveats because the PictureBox control has no need to process the message and there's no risk that you will break existing behaviors.



Sub PictBox_MouseDown(ByVal sender As Object, _ ByVal e As MouseEventArgs) _ Handles MyBase.MouseDown Dim obj As New DataObject obj.SetData(DataFormats.Bitmap, Me.Image) obj.SetData(DataFormats.Metafile, Me.Image) Dim eff As DragDropEffects eff = DoDragDrop(obj, DragDropEffects.Copy) End Sub

To pack data, you can use an instance of the DataObject class. This class provides a default (but recommended) implementation of the IDataObject interface. To add data, you call SetData one or more times, each time indicating a value and a data format. The same information can be stored in various formats to reach a wide range of clients. In the example above, the image is saved as a bitmap and metafile. This simple thing makes it possible for you to drag from the TextBox to a PowerPoint slide. Go ahead and believe!

Drag-and-drop is not reputed to be a feature that makes your application richer from a functional point of view. Perhaps this is a debatable point. What drag-and-drop does is make applications easier and more intuitive to use. In this article, I've discussed the basics of drag-and-drop in the .NET Framework, and focused on the members, the classes, and the model that makes it work. Next, I've provided a few practical examples including how to enhance TextBox and PictureBox controls to accept drops and drag their data out.

Although drag-and-drop can be easily implemented within the context of an application, my advice is to insulate the code in the body of individual controls by building a custom library of components that know how to accept this intuitive and immediate form of input.



Dino Esposito is a mentor at Solid Quality Mentors where he manages the ASP.NET, workflow, and AJAX courseware. A speaker at many industry events including Microsoft TechEd, Basta, DevWeek, and DevConnections, Dino is the author of two volumes of Programming Microsoft ASP.NET 2.0 Applications, for Microsoft Press. You can find late breaking news at http://weblogs.asp.net/despos.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date