???? ID: 307966 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

????? ??? ????? C# ????????? ??? ?????-????-????? ??????????? ?????? ???? ?? ??? ???? ????

?????? ??????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.

?? ????? ??

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

??????

?? ???? ??? ??? ?? ??? ?? ??? ????????? ????????? ???? ?? ?? ????? ???? ????? C# ????????? ??? ?????-????-????? ??????????? ?????? ???? ?? ??? ???? ????? AListBox???????? ????? ?????-????-????? ????????? ?? ?????? ?? ??? ??? ????? ???? ???? ???

??????????

?? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • ????? C#
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:

  • Windows FormsListBoxcontrol
  • Windows Forms event handling

Steps to Build the Sample

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???????? ??? ?????? ??????? ?? ???????? whatever ??? ???? ?? ??? ?? ?? ????? ?? ?? ??????? ?? ????? ?? ????? ?????? ?? ???, ?? ??? ???? ?? ????? ?? ???? ???System.IO??? ????? ????? ?? ??????? ?? ????? ?? ??? ??????? ?? ?? ????, ?? ???? ?? ????? ?? ??? ??????? ?? ????????? ?????? ?? ?????? ???, ?? ??? ?????? ???? ?? ?? ??? ????? ??????? ?? ???ListBox???????? ???

????? ??? ????? C# ????????? ??? ?????-????-????? ??????????? ??????, ?? ????? ?? ???? ????:
  1. ????? C# .NET ?? Visual C# 2005 ??? ?? ??? ??????? ?? Windows ????????? ?? ?????? Form1 ???????? ??? ?? ???? ??..
  2. ????? ?? ??? ????? ?? ????? ????ListBoxForm1 ????????? ?????
  3. ??? ????? ???, ??? ?????AllowDrop?? ???ListBox???? ?? ??? ????????True???????? ???????? ?? ????? ???? ????
  4. ?????? Explorer ???, Form1 ?? ????-????? ????, ?? ???? ??? ????? ????????? ???.
  5. ????? ???? ?? ???DragEnter????? ?? ???, ????? ??? ??? Windows ??????? ??????? Form1 ???? ??? ????? ?? ?? ????? ???? ??????:
    private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
    {
    	if(e.Data.GetDataPresent(DataFormats.FileDrop))
    		e.Effect = DragDropEffects.All;
    	else
    		e.Effect = DragDropEffects.None;
    }
    					
  6. ????? ???? ?? ???DragDrop?????, Form1 ???? ????? ???? ?? ??? 5 ??? ????? ??? ?? ????? ?? ??? ????? ???? ??????:
    private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
    	string[] s = (string[]) e.Data.GetData(DataFormats.FileDrop, false);
    	int i;
    	for(i = 0; i < s.Length; i++)
    		listBox1.Items.Add(s[i]);
    }
    					
  7. ?? ????? handlers ???????? ??????? ?? ??? ?????? ????, ?? ??? ????? ??? ??? ?????? Form1 constructor ??? ???? ?? ???InitializeComponent:
    this.listBox1.DragDrop += new
               System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop);
    this.listBox1.DragEnter += new
               System.Windows.Forms.DragEventHandler(this.listBox1_DragEnter);
    
    					
  8. ?????, ?? ????????? ?? ??????
  9. ?? ?? ???? ??????? ?? ???????? ?? ????? ?? ?????? ???? ?? ??? ??????ListBox???????? ??? ??? ??????? ?? ???? ?? ??? ????? ??ListBox???????? ???

??????

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

Control.DragDrop ?????
(VS.71) http://MSDN.Microsoft.com/en-us/library/SYSTEM.Windows.forms.control.dragdrop .aspx (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragdrop(vs.71).aspx)

???? ???? ???? ??:
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
??????: 
kbsweptvs2008 kbhowtomaster kbsample kbmt KB307966 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:307966  (http://support.microsoft.com/kb/307966/en-us/ )