topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 2:41 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Open a File by dropping it onto Form (C# example)  (Read 3053 times)

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • View Profile
    • Donate to Member
Open a File by dropping it onto Form (C# example)
« on: January 13, 2011, 06:52 PM »
Since I researched and implemented this for a recent short project, I thought I'd make it available in case it might benefit any other C# developers:

Sample Code for Opening a File Dropped onto Form.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Open a File by dropping it onto Form (C# example)
« Reply #1 on: January 13, 2011, 06:55 PM »
In case you're going to semi-regularly post code snippets, you should check out SyntaxHighlighter - it's nice :)
- carpe noctem

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • View Profile
    • Donate to Member
Re: Open a File by dropping it onto Form (C# example)
« Reply #2 on: January 13, 2011, 07:17 PM »
Thanks for the link.  I also like Veign's Code Highlighter.

Of course, I can also just use the site's code tags:

Code: C# [Select]
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Diagnostics;
  4. using System.IO;
  5.  
  6. namespace BryanWinForms1 {
  7.     public partial class Form1 : Form {
  8.  
  9.         private delegate void DelegateOpenFile(String s); //define delegate type
  10.         DelegateOpenFile _openFileDelegate; //declares a delegate instance
  11.  
  12.         public Form1() {
  13.             InitializeComponent();
  14.             this.AllowDrop = true; //must set this to true, for dragDrop to work
  15.                         //delegate needed so Form1_DragDrop() can asynchronously
  16.                         //invoke our program's OpenFile() method
  17.             _openFileDelegate = new DelegateOpenFile(this.OpenFile); //instantiates delegate
  18.         }
  19.  
  20.         private void openToolStripMenuItem_Click(object sender, EventArgs e) {
  21.             OpenFileDialog openDlg = new OpenFileDialog();
  22.             openDlg.Filter = "Any File (*.*)|*.*";
  23.             openDlg.FileName = "";
  24.             openDlg.CheckFileExists = true;
  25.             openDlg.CheckPathExists = true;
  26.  
  27.             if (openDlg.ShowDialog() != DialogResult.OK)
  28.                 return;
  29.  
  30.             OpenFile(openDlg.FileName);
  31.         }
  32.  
  33.         private void OpenFile(string sFile) {
  34.             //insert appropriate file-opening code here...
  35.             MessageBox.Show("\"" + sFile + "\" will be opened.");
  36.         }
  37.  
  38.         private void Form1_DragEnter(object sender, DragEventArgs e) {
  39.                         //we're only interested if a FILE was dropped on the form
  40.             if (e.Data.GetDataPresent(DataFormats.FileDrop))
  41.                 e.Effect = DragDropEffects.Copy;
  42.             else
  43.                 e.Effect = DragDropEffects.None;
  44.         }
  45.  
  46.         private void Form1_DragDrop(object sender, DragEventArgs e) {
  47.                         //good idea to use try-catch block, in case something goes wrong
  48.                         try {
  49.                 Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
  50.                 if (a != null) {
  51.                     // Extract string from first array element
  52.                     // (ignore all files except first if number of files are dropped).
  53.                     string s = a.GetValue(0).ToString();
  54.                     // Call OpenFile asynchronously.
  55.                     // Explorer instance from which file is dropped is not responding
  56.                     // the entire time that the DragDrop handler is active, so we need to return
  57.                     // immidiately (especially if OpenFile shows MessageBox).
  58.                     this.BeginInvoke(_openFileDelegate, new Object[] { s });
  59.                     this.Activate();        // in the case Explorer overlaps this form
  60.                 }
  61.             }
  62.             catch (Exception ex) {
  63.                 Trace.WriteLine("Error in DragDrop function: " + ex.Message);
  64.                 // don't show MessageBox here - Explorer is waiting !
  65.             }
  66.         }
  67.                
  68.                 //next right-brace ends Form1 class
  69.     }
  70.         //next right-brace ends namespace
  71. }