2009-09-02 11 views

Odpowiedz

16

Napisałem ten kod oparty na tym 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 ma różne zdarzenia do radzenia sobie z przeciąganiem/upuszczaniem - prawdopodobnie będziesz potrzebować spojrzeć na zdarzenie DragDrop tylko po to, co chcesz.

4

Set AllowDrop na true na swojej TextBox i wpisz następujący kod dla wydarzeń dragdrop i 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

Jeśli masz poniżej komunikatów o błędach, to stosuje się do mnie, kiedy przy użyciu programu Visual Studio 2015, spróbuj e.Effect zamiast e.Effects

Kod Istotność Opis projektu State Line pliku Tłumienie Błąd CS1061 „DragEventArgs” nie zawiera definicji "Efekty" ani metody rozszerzającej "Efekty" można znaleźć akceptację pierwszego argumentu typu "DragEventArgs" (czy brakuje dyrektywy użycia lub odniesienia do zespołu?)

Powiązane problemy