???? ID: 306969 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

How to provide file drag-and-drop functionality in a Visual Basic 2005 or Visual Basic .NET application

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
?? Microsoft Visual C# .NET ??????? ?? ??? ?? ???? ??, ?????307966  (http://support.microsoft.com/kb/307966/ ) .

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

The step-by-step procedure in this article demonstrates how to provide file drag-and-drop functionality in a Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET application. AListBoxcontrol is used as the destination of the file drag-and-drop procedure.

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • Visual Basic 2005 or Visual Basic .NET
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • Windows ???????ListBoxcontrol
  • Windows Forms event handling

????? ????? ?? ??? ???

TheListBoxcontrol provides two drag-and-drop events that you need to handle:DragEnter, ??DragDrop. TheDragEnterevent occurs when you drag an object within the bounds of the control and is used to determine whether the object that is being dragged is one that you want to allow to be dropped on the control. You handle this event for cases in which a file or files are dragged to the control. This allows the appropriate icon to be displayed when the object is dragged over the control, depending on the object that is being dragged. TheDragDropevent occurs when the object that is being dragged has been released on the control. You handle this event to retrieve the object. The????:object is used to retrieve the data.

The????:object'sGetDatamethod returns an array of strings that contain the full path names of the files that were dragged to theListBox???????? ??? You can use this file path information to perform whatever operations are needed on the files. For example, you can use classes in theSystem.IOnamespace to open and read the files, move the files, or copy the files to a new location. This sample only adds the full path to the files that are dragged to theListBox???????? ???

????? ???? Visual Basic 2005 ?? Visual Basic .NET ????????? ??? ?????-????-????? ??????????? ??????, ?? ????? ?? ???? ????:
  1. Visual Basic 2005 ?? Visual Basic .NET ??? ?? ?? Windows ??????? ????????? ?? ?????? Form1 ???????? ??? ?? ???? ??..
  2. ????? ?? ??? ????? ?? ????? ????ListBoxForm1 ????????? ?????
  3. ??? ????? ???, ??? ?????AllowDrop?? ???ListBox???? ?? ??? ????????True???????? ???????? ?? ????? ???? ????
  4. ?????? Explorer ???, Form1 ?? ????-????? ????, ?? ???? ??? ????? ????????? ???.
  5. ????? ???? ?? ???DragEnter????? ?? ???, ????? ??? ??? Windows ??????? ??????? Form1 ???? ??? ????? ?? ?? ????? ???? ??????:
        Private Sub ListBox1_DragEnter(ByVal sender As Object, _
                                       ByVal e As System.Windows.Forms.DragEventArgs) _
                                       Handles ListBox1.DragEnter
            If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
                e.Effect = DragDropEffects.All
            Else
                e.Effect = DragDropEffects.None
            End If
        End Sub
    					
  6. ????? ???? ?? ???DragDrop?????, Form1 ???? ????? ???? ?? ??? 5 ??? ????? ??? ?? ????? ?? ??? ????? ???? ??????:
        Private Sub ListBox1_DragDrop(ByVal sender As Object, _
                                      ByVal e As System.Windows.Forms.DragEventArgs) _
                                      Handles ListBox1.DragDrop
            Dim s() As String = e.Data.GetData("FileDrop", False)
            Dim i As Integer
            For i = 0 To s.Length - 1
                ListBox1.Items.Add(s(i))
            Next i
        End Sub
    					
  7. ?????, ?? ????????? ?? ??????
  8. ?? ?? ???? ??????? ?? ???????? ?? ????? ?? ?????? ???? ?? ??? ??????ListBox???????? ??? ??? ??????? ?? ???? ?? ??? ????? ??ListBox???????? ???

??????

???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ?????? ?? ????::
(vs.71) http://msdn2.Microsoft.com/en-us/library/SYSTEM.Windows.forms.control.dragenter .aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dragenter(vs.71).aspx)

(vs.71) http://msdn2.Microsoft.com/en-us/library/SYSTEM.Windows.forms.control.dragdrop .aspx (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dragdrop(vs.71).aspx)

???? ???? ???? ??:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbvs2005swept kbvs2005applies kbhowtomaster kbmt KB306969 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:306969  (http://support.microsoft.com/kb/306969/en-us/ )