Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


SSWUG


Reader rating:
Click here to read 3 comments about this article.
Article source: CoDe (2008 Mar/Apr)


Article Pages: < Previous - 1 2 3 4  5 


Implementing Drag and Drop in Your Windows Application (Cont.)

Implementing Drag and Drop for Custom Objects

Most Windows Forms controls support the set (or subset) of events that I described in this article for drag and drop operations. However, what happens if the control you want to enable for drag and drop does not support the list of events that I have just described? A good example is the Windows Media Player ActiveX control. You might want to embed the Windows Media Player control in a Windows application so that users can simply drag and drop media files onto it to play. The Windows Media Player ActiveX control by itself does not support events like DragEnter and DragDrop, and hence there is no easy way to implement drag and drop.

A workaround is to wrap the ActiveX control using a User control. First, add a new User Control item (right-click the project name in Solution Explorer and select Add > New Item… > select User Control) to the existing project. Name the file as MediaPlayer.vb.

Right-click the Toolbox and select Choose Items…. In the Choose Toolbox Items dialog box, click the COM Components tab and check the Windows Media Player object (Figure 10). Click OK to add the Windows Media Player control onto the Toolbox.

Click for a larger version of this image.

Figure 10: Adding the Windows Media Player ActiveX control to the Toolbox.

Drag the Windows Media Player control from the Toolbox and drop it onto the MediaPlayer.vb design surface (Figure 11).

Click for a larger version of this image.

Figure 11: Populate the user control with the Windows Media Player control.

In the code behind of MediaPlayer.vb, code the following:

Public Class MediaPlayer
    Private _URL As String
    Public Property URL() As String
        Get
            Return _URL
        End Get
        Set(ByVal value As String)
            _URL = value
            AxWindowsMediaPlayer1. _
               URL = _URL
        End Set
    End Property
End Class

Essentially you expose the URL property to let the user of this control set the URL of the media file to play.

Right-click the project name in Solution Explorer and select Build. The MediaPlayer control should now appear in the toolbox (Figure 12).

Click for a larger version of this image.

Figure 12: The MediaPlayer control in the Toolbox.

Drag and drop the MediaPlayer user control onto Form1 (Figure 13).

Click for a larger version of this image.

Figure 13: Adding the MediaPlayer control to Form1.

Switch to the code behind of Form1 and handle the DragEnter event of the MediaPlayer user control (Listing 14).

Finally, handle its DragDrop event so that you can play the media file dropped by the user (Listing 15).

Note that since the user can drop multiple files onto the control, you will only load the first file using the MediaPlayer control. Figure 14 shows the MediaPlayer control hosted in a Windows Form playing the file dropped onto it.

Click for a larger version of this image.

Figure 14: Dragging and dropping a media file onto Form1.

Dragging and Dropping Custom Objects

So far in this article I’ve shown you how to use the various data types as specified in the DataFormats class: Bitmap, CommaSeparatedValue, Dib, Dif, EnhancedMetafile, FileDrop, Html, Locale, MetafilePict, OemText, Palette, PenData, Riff, Rtf, Serializable, StringFormat, SymbolicLink, Text, Tiff, UnicodeText, and WaveAudio. What happens if you want to drag and drop data of a specific type? For example, you might want to drag an item in a ListView control. In this case, the DragEnter event will look something like this:

If (e.Data.GetDataPresent _
   ("System.Windows.Forms. _
   ListViewItem()")) Then
    '---determine if this is a copy 
    '---or move---
    If (e.KeyState And CtrlMask) = 
    CtrlMask Then
        e.Effect = 
        DragDropEffects.Copy
    Else
        e.Effect = 
        DragDropEffects.Move
End If

The MouseDown event handler will now look like this:

Control.DoDragDrop(New _   
DataObject("System.Windows.Forms.
  ListViewItem()", _
Items), DragDropEffects.Move Or _
DragDropEffects.Copy)

Summary

It is not really difficult to implement drag and drop functionality in your Windows application. All you need is to understand the type of data you want to support and make the necessary provisions for dealing with that particular data type.

Wei-Meng Lee

&


Listing 14: Servicing the DragEnter event for the MediaPlayer control
 Private Sub MediaPlayer1_DragEnter( _
   ByVal sender As Object, _
   ByVal As System.Windows.Forms.DragEventArgs) _
   Handles MediaPlayer1.DragEnter

   '---if the data to be dropped is 
   ' an filedrop format---
   If (e.Data.GetDataPresent( _
      DataFormats.FileDrop)) Then

        '---determine if this is a copy or move---
        If (e.KeyState And CtrlMask) = CtrlMask Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.Move
        End If
    End If
End Sub


Listing 15: Servicing the DragDrop event for the MediaPlayer control
Private Sub MediaPlayer1_DragDrop( _
   ByVal sender As Object, _
   ByVal As System.Windows.Forms. _
   DragEventArgs) _
   Handles MediaPlayer1.DragDrop

   If (e.Data.GetDataPresent( _
      DataFormats.FileDrop)) Then
        Dim files() As String

        '---get all the file names---
        files = e.Data.GetData( _
           DataFormats.FileDrop)

        If files.Length Then

            '---load only the first file---
            files(0) = UCase(files(0))
            If files(0).EndsWith(".WMV") Then

                '---get the media player to play the 
                ' first file---
                MediaPlayer1.URL = files(0)
            End If
        End If
    End If
End Sub


Article Pages: < Previous - 1 2 3 4  5 

Page 1: Implementing Drag and Drop in Your Windows Application
Page 2: Drag and Drop Text
Page 3: Drag and Drop Images
Page 4: Move versus Copy
Page 5: Implementing Drag and Drop for Custom Objects

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
4 out of 5

11 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      AppsWorld Europe

 

SSWUG