From c31b9fb57e37069145bca158a4312dbd6a5b6fd2 Mon Sep 17 00:00:00 2001 From: Luv Letter Date: Tue, 11 Jun 2019 00:35:04 +0800 Subject: [PATCH] * Support Drag txt File --- Form1.Designer.cs | 5 +++-- Form1.cs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Form1.Designer.cs b/Form1.Designer.cs index f7d7ba6..8faa9d1 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -212,6 +212,7 @@ private void InitializeComponent() // // lst_File // + this.lst_File.AllowDrop = true; this.lst_File.FormattingEnabled = true; this.lst_File.ItemHeight = 15; this.lst_File.Location = new System.Drawing.Point(12, 136); @@ -220,6 +221,8 @@ private void InitializeComponent() this.lst_File.Size = new System.Drawing.Size(252, 484); this.lst_File.TabIndex = 17; this.lst_File.SelectedIndexChanged += new System.EventHandler(this.lst_File_SelectedIndexChanged); + this.lst_File.DragDrop += new System.Windows.Forms.DragEventHandler(this.Lst_File_DragDrop); + this.lst_File.DragEnter += new System.Windows.Forms.DragEventHandler(this.Lst_File_DragEnter); // // lst_SubTitle // @@ -322,8 +325,6 @@ private void InitializeComponent() this.Text = "Form1"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.Load += new System.EventHandler(this.Form1_Load); - //this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); - // override protected method `ProcessCmdKey` this.ResumeLayout(false); this.PerformLayout(); diff --git a/Form1.cs b/Form1.cs index 8649233..65b2e54 100644 --- a/Form1.cs +++ b/Form1.cs @@ -203,6 +203,7 @@ private void btn_DeleteFile_Click(object sender, EventArgs e) private void btn_import_Click(object sender, EventArgs e) { + Thread.CurrentThread.SetApartmentState(ApartmentState.STA); var ofd = new OpenFileDialog(); // ofd.Reset(); ofd.Filter = "Txt File (*.txt)|*.txt|All Files|*.*"; @@ -225,6 +226,7 @@ private void btn_import_Click(object sender, EventArgs e) } } } + ofd.Dispose(); } private void lst_File_SelectedIndexChanged(object sender, EventArgs e) @@ -324,5 +326,33 @@ private void btn_Clear_Click(object sender = null, EventArgs e = null) } #endregion + private void Lst_File_DragEnter(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(DataFormats.FileDrop)) + e.Effect = DragDropEffects.All; + else + e.Effect = DragDropEffects.None; + } + + private void Lst_File_DragDrop(object sender, DragEventArgs e) + { + string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false); + + foreach (string filePath in s) + { + Console.WriteLine(filePath); + if (File.Exists(filePath)) + { + if (Path.GetExtension(filePath) != ".txt") + continue; + string fileName = Path.GetFileName(filePath); + if (!lst_File.Items.Contains(fileName)) + { + lst_File.Items.Add(fileName); + ST_Files.Add(fileName, filePath); + } + } + } + } } }