国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

c#中實現(xiàn)文件拖放打開的方法

2020-01-18 01:47:54
字體:
供稿:網(wǎng)友

C#實現(xiàn)文件拖放并打開文件

需要知道的ListBox的兩個事件:當您在控件的邊界內(nèi)拖動對象時,便會發(fā)生 DragEnter 事件;該事件用于確

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

得在將對象拖到控件上方時,能夠根據(jù)所拖動的對象顯示相應的圖標。 將拖動的對象釋放到控件上時,會發(fā)生

DragDrop 事件。

功能描述:向ListBox拖入一個文件,ListBox顯示該文件的路徑,然后單擊該路徑,點擊Open按鈕打開該文件。



代碼實現(xiàn):

需要將ListBox的AllowDrop屬性改為true,并實現(xiàn)它的DragEnter、DragDrop這兩個事件。

復制代碼 代碼如下:

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內(nèi)的值是否被選中。
        /// </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 方法返回一個字符串數(shù)組,該數(shù)組包含拖到該列表框控件中的文件的完整路徑名。 可以使用此文件路徑信息來執(zhí)行需要對文件執(zhí)行的任何操作。

補充代碼:

復制代碼 代碼如下:
private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) 
  { 
  if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
  e.Effect = DragDropEffects.Link;  
  else e.Effect = DragDropEffects.None;  
  } 

  private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) 
  { 
  //其中l(wèi)abel1.Text顯示的就是拖進文件的文件名; 
  label1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();  
  }  

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 承德县| 蒲城县| 镇江市| 台东县| 唐海县| 九寨沟县| 巴塘县| 讷河市| 樟树市| 贡觉县| 潜江市| 贡觉县| 湛江市| 大港区| 舟曲县| 华亭县| 桑日县| 乐陵市| 鄂托克前旗| 离岛区| 横山县| 松滋市| 佳木斯市| 大关县| 凌源市| 株洲市| 盈江县| 晋州市| 海兴县| 迁西县| 新邵县| 长寿区| 文山县| 镇平县| 松溪县| 临泉县| 崇礼县| 从化市| 镇江市| 郸城县| 连山|