2009-09-02 38 views

Trả lời

16

i đã viết mã này có trụ sở tại này link

public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     textBox1.AllowDrop = true; 
     textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter); 
     textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop); 

    } 

    private void textBox1_DragEnter(object sender, DragEventArgs e) 
    { 
     if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
     e.Effects = DragDropEffects.Copy; 
     else 
     e.Effects = DragDropEffects.None; 
    } 

    private void textBox1_DragDrop(object sender, DragEventArgs e) 
    { 
     string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false); 


    string s=""; 

    foreach (string File in FileList) 
    s = s+ " "+ File ; 
    textBox1.Text = s; 
    } 
    } 
0

Control có nhiều sự kiện khác nhau để xử lý thao tác kéo/thả - có thể bạn sẽ chỉ cần xem sự kiện DragDrop cho những gì bạn muốn.

4

Set AllowDrop true vào TextBox của bạn, và viết đoạn code sau cho các sự kiện DragDrop và DragEnter:

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

    private void textBox1_DragDrop(object sender, DragEventArgs e) 
    { 
     if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
     { 
      string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop); 
      textBox1.Lines = fileNames; 
     } 
    } 
0

CodeProject has a really nice example để làm điều này , bao gồm cách cho phép kéo và thả cả hai cách (từ Explorer đến ứng dụng của bạn và từ ứng dụng của bạn đến Explorer).

0

Nếu bạn nhận được thông báo lỗi dưới đây, này áp dụng đối với tôi khi sử dụng Visual Studio 2015, thử e.Effect thay vì e.Effects

Mức độ nghiêm trọng Mã hàng Mô tả dự án Tập Dòng Suppression Nhà nước Lỗi CS1061 'DragEventArgs' không chứa định nghĩa cho 'Hiệu ứng' và không có phương pháp mở rộng 'Hiệu ứng' nào chấp nhận đối số đầu tiên của loại 'DragEventArgs' có thể được tìm thấy (bạn thiếu một chỉ thị sử dụng hoặc tham chiếu assembly?)

Các vấn đề liên quan