C#實現檔案拖放並開啟檔案

來源:互聯網
上載者:User

 

C#實現檔案拖放並開啟檔案

需要知道的ListBox的兩個事件:當您在控制項的邊界內拖動對象時,便會發生 DragEnter 事件;該事件用於確

定當前拖動的對象是不是您要放到控制項上的對象。 在將一個或多個檔案拖到控制項上時,需要處理此事件。 這使

得在將對象拖到控制項上方時,能夠根據所拖動的對象顯示相應的表徵圖。 將拖動的對象釋放到控制項上時,會發生

DragDrop 事件。

功能描述:向ListBox拖入一個檔案,ListBox顯示該檔案的路徑,然後單擊該路徑,點擊Open按鈕開啟該檔案。

 

代碼實現:

需要將ListBox的AllowDrop屬性改為true,並實現它的DragEnterDragDrop這兩個事件。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace DragDrop{    public partial class DragDrop : Form    {        public string FilePath;        public DragDrop()        {            InitializeComponent();        }        /// <summary>        /// 擷取ListBox的值。        /// </summary>        /// <returns></returns>        public string GetListBoxItem()        {            string filePath = string.Empty;                        bool isSelected = IsListBoxSelected();            if (isSelected==true)            {                string listBoxItemValue = lbFilePath.SelectedItem.ToString();                filePath = listBoxItemValue;            }            else            {                MessageBox.Show("ListBox must be selected.");            }            return filePath;        }        /// <summary>        /// ListBox內的值是否被選中。        /// </summary>        /// <returns></returns>        public bool IsListBoxSelected()        {            bool selected;            if (lbFilePath.SelectedIndex == -1)//SelectedIndex==-1時,表示未選中任何項。            {                selected = false;            }            else            {                selected = true;            }            return selected;        }        private void lbFilePath_DragEnter(object sender, DragEventArgs e)        {            if (e.Data.GetDataPresent(DataFormats.FileDrop))            {                e.Effect = DragDropEffects.All;            }            else            {                e.Effect = DragDropEffects.None;            }        }        private void lbFilePath_DragDrop(object sender, DragEventArgs e)        {            string[] s=(string[])e.Data.GetData(DataFormats.FileDrop,false);                        for (int i = 0; i < s.Length; i++)            {                lbFilePath.Items.Add(s[i]);            }        }        private void btnOpenFile_Click(object sender, EventArgs e)        {            string filePath=GetListBoxItem();            if (!string.IsNullOrEmpty(filePath))            {                System.Diagnostics.Process.Start(filePath);            }        }    }}

 

總結:

Data 對象的 GetData 方法返回一個字串數組,該數組包含拖到該列表框控制項中的檔案的完整路徑名。 可以使用此檔案路徑資訊來執行需要對檔案執行的任何操作。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.