c#实现文件的拖放拖拽[解决不响应的问题]

Jan 14th, 2010

如果在google上搜索 “c# 拖放文件”,可以搜索到很多答案,但是完全按照上面的方法去做又会发现程序没有响应。几乎所有的解答都会给出如下的答案:

 


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

 

同时,有部分答案还告知,要打开AllowDrop开关。 事实上这两步都没有错。但是这样做下来都还是无法响应。

 

还有一步这些答案都没有告知,那就是要把事件关联起来。进入Event列表。 Event列表在那呢,看见属性列表上的那个闪电标记了吧,那就是事件栏,点击它找到列表中的DragDrop和DragEnter两项,分别设置为Form1_DragDrop和Form1_DragEnter,也就是代码中的两个function. 然后保存就能够实现拖拽了。

Share on Facebook
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • Fleck
  • Gwar
  • Haohao
  • Identi.ca
  • laaik.it
  • LinkaGoGo
  • LinkedIn
  • Linkter
  • Live
  • MisterWong
  • MisterWong.DE
  • MSN Reporter
  • MySpace
  • PDF
  • Ping.fm
  • RSS
  • Slashdot
  • Socialogs
  • Technorati
  • Tumblr
  • Twitter
  • Twitthis
  • Webride
  • Yahoo! Bookmarks
  • Yigg
Tags: , ,
No comments yet.
Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).