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

定當前拖動的對象是不是您要放到控件上的對象。 在將一個或多個文件拖到控件上時,需要 " /> 亚洲欧美bt,清纯唯美日韩制服另类,7777精品伊人久久久大香线蕉的

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

c#中實現文件拖放打開的方法

C#實現文件拖放并打開文件

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

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

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

DragDrop 事件。

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



代碼實現:

需要將ListBox的AllowDrop屬性改為true,并實現它的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內的值是否被選中。
        /// </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 方法返回一個字符串數組,該數組包含拖到該列表框控件中的文件的完整路徑名。 可以使用此文件路徑信息來執行需要對文件執行的任何操作。

補充代碼:

復制代碼 代碼如下: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) 
  { 
  //其中label1.Text顯示的就是拖進文件的文件名; 
  label1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();  
  }  

AspNet技術c#中實現文件拖放打開的方法,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 远安县| 太原市| 敦煌市| 金坛市| 会宁县| 通化县| 长汀县| 平远县| 拉萨市| 昌吉市| 湖北省| 龙海市| 岳普湖县| 萨迦县| 石城县| 仪征市| 松滋市| 峡江县| 广州市| 开江县| 澄城县| 大同市| 青冈县| 莎车县| 临海市| 渭源县| 陆良县| 通城县| 乳山市| 耒阳市| 台湾省| 周口市| 赣榆县| 阿克陶县| 安义县| 商洛市| 马边| 介休市| 肥城市| 临海市| 青神县|