• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

c#中实现文件拖放打开的方法

asp 搞代码 4年前 (2022-01-03) 38次浏览 已收录 0个评论

向ListBox拖入一个文件,ListBox显示该文件的路径,然后单击该路径,点击Open按钮打开该文件

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();
        }

        ///

        /// 获取ListBox的值。
        ///

        ///
        public string GetListBoxItem()
        {
            string filePath = string.Empty;

            bool isSelected = IsListBoxSelected();

            if (isSelected==true)
            {
                string listBox来源gao@!dai!ma.com搞$$代^@码网ItemValue = lbFilePath.SelectedItem.ToString();

                filePath = listBoxItemValue;
            }
            else
            {
                MessageBox.Show(“ListBox must be selected.”);
            }

            return filePath;
        }

        ///

        /// ListBox内的值是否被选中。
        ///

        ///
        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);
            }
        }
    }
}

以上就是c#中实现文件拖放打开的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:c#中实现文件拖放打开的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址