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

定當(dāng)前拖動的對象是不是您要放到控件上的對象。 在將一個或多個文件拖到控件上時,需要 " /> 久久久久久午夜,天天影视综合,在线播放亚洲

中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

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

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

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

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

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

DragDrop 事件。

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



代碼實現(xiàn):

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

復(fù)制代碼 代碼如下:
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);
            }
        }
    }
}

總結(jié):

Data 對象的 GetData 方法返回一個字符串?dāng)?shù)組,該數(shù)組包含拖到該列表框控件中的文件的完整路徑名。 可以使用此文件路徑信息來執(zhí)行需要對文件執(zhí)行的任何操作。

補充代碼:

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

AspNet技術(shù)c#中實現(xiàn)文件拖放打開的方法,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 欧美一区二区免费电影 | 免费视频一区二区 | 在线免费亚洲视频 | av特级毛片 | 美女久久 | 久久九精品 | 国产欧美一级二级三级在线视频 | 国精品一区 | 黄 色 毛片免费 | 日韩一区二区三区在线播放 | 日韩精品亚洲专区在线观看 | 久久久毛片 | 在线看免费的a | 天天干视频 | 成人在线h | 在线一区二区三区 | 亚洲欧美在线视频 | 一区二区日韩 | 99久久国产综合精品麻豆 | 久久伊人一区二区 | 亚洲国产中文字幕 | 亚洲高清成人在线 | 久久涩涩 | 欧美亚洲一区二区三区 | 久久国产精品无码网站 | 日本成人中文字幕 | 亚洲天堂精品一区 | 欧美 中文字幕 | 亚洲欧美日韩电影 | av影音资源| 天天操综合网 | 精品视频在线免费观看 | 一区视频 | 欧美精品国产一区二区 | 欧美在线天堂 | 天天拍天天操 | 黑人精品| 中文字幕亚洲区 | 在线观看av中文字幕 | 亚洲视频免费观看 | 免费看黄视频网站 |