首页 >> 文档 >> C#专题

C#实现拖拽功能的代码

发布日期:2008-07-10最近更新:2008-07-10来源:BHCODE作者:

C#实现拖拽功能的代码:

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;
using System.Diagnostics;

namespace Walter.K.Wang
{
    public partial class Form1 : Form
    {
        private Process process = new Process();
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files;
            files = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (string file in files)
            {
                listBox1.Items.Add(file);
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                process.StartInfo.FileName = Convert.ToString(listBox1.SelectedItem);
                process.Start();
            }
        }
    }
}

本周推荐
MORE
热点关注
MORE