已有56人关注
拖放不上数据?
发表在C#图书答疑 2019-05-24 悬赏:5 学分 《C#精彩编程200例》第2章 文件操作 100页-101页
是否精华
版块置顶:

和源码程序一样,源码能拖放数据,我的不能?请老师指教。谢谢!

源码程序为:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Linq;

using System.Windows.Forms;


namespace AllowDropFile

{

    public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            this.AllowDrop = true;


        }


        private void Form1_DragEnter(object sender, DragEventArgs e)

        {

            if (e.Data.GetDataPresent(DataFormats.FileDrop))

            {

                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); //获取拖入文件的基本信息

                for (int i = 0; i < files.Length; i++) //拖放入窗体的文件的文件名加入ListBox

                {

                    listBox1.Items.Add(files[i]); //添加文件的路径

                }

            }

        }

    }

}

运行后:image.png

我的程序为:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace AllowDropFile

{

    public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)

        {

            this.AllowDrop = true;

        }


        private void Form1_DragDrop(object sender, DragEventArgs e)

        {

            if(e.Data.GetDataPresent(DataFormats.FileDrop))

            {

                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                for(int i=0;i<files.Length;i++)

                {

                    listBox1.Items.Add(files[i]);

                }

            }

        }

    }

}

运行后:image.png

分享到:
精彩评论 1
小飞侠
学分:358 LV5
2019-05-24
沙发

解决了,原来是事件弄错了,Form1_DragEnter错写成了Form1_DragDrop,哈哈!

首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照