2014-02-11 59 views
0

我嘗試的選項添加到我的應用程序拖動文件到我Listbox而不是瀏覽到該文件夾​​,這是我嘗試:拖放文件到我的列表框

private void Form1_Load(object sender, EventArgs e) 
{ 
    listBoxFiles.AllowDrop = true; 
    listBoxFiles.DragDrop += listBoxFiles_DragDrop; 
    listBoxFiles.DragEnter += listBoxFiles_DragEnter; 
} 

private void listBoxFiles_DragEnter(object sender, DragEventArgs e) 
{ 
    e.Effect = DragDropEffects.Copy; 
} 

private void listBoxFiles_DragDrop(object sender, DragEventArgs e) 
{ 
    listBoxFiles.Items.Add(e.Data.ToString()); 
} 

但代替完整的文件路徑e.Data.ToString()return System.Windows.Forms.DataObject

回答

2

此代碼,我發現here

private void Form1_Load(object sender, EventArgs e) 
{ 
    listBoxFiles.AllowDrop = true; 
    listBoxFiles.DragDrop += listBoxFiles_DragDrop; 
    listBoxFiles.DragEnter += listBoxFiles_DragEnter; 
} 

private void listBoxFiles_DragEnter(object sender, DragEventArgs e) 
{ 
    if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy; 
} 

private void listBoxFiles_DragDrop(object sender, DragEventArgs e) 
{ 
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); 
    foreach (string file in files) 
     listBoxFiles.Items.Add(file); 
}