Welcome Guest.   Make a donation to an author on the site September 01, 2014, 06:24:34 PM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
View the new Member Awards and Badges page.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: Open a File by dropping it onto Form (C# example)  (Read 1075 times)
kyrathaba
N.A.N.Y. Organizer
Honorary Member
**
Posts: 3,010



while(! dead_horse){beat}

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« on: January 13, 2011, 06:52:25 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.
Logged

Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 26.0
_________________________________________________________________________________________

I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF

My DC page: http://kyrathaba.dcmembers.com | My blog: http://williambryanmiller.com/ | Proofreading Service: http://bit.ly/1fQSqQP

f0dder
Charter Honorary Member
***
Posts: 8,774



[Well, THAT escalated quickly!]

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #1 on: January 13, 2011, 06:55:44 PM »

In case you're going to semi-regularly post code snippets, you should check out SyntaxHighlighter - it's nice smiley
Logged

- carpe noctem
kyrathaba
N.A.N.Y. Organizer
Honorary Member
**
Posts: 3,010



while(! dead_horse){beat}

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #2 on: January 13, 2011, 07:17:57 PM »

Thanks for the link.  I also like Veign's Code Highlighter.

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

Formatted for C# with the GeSHI Syntax Highlighter [copy or print]
  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. }
Logged

Win 7 Home Premium 64bit-SP1 AMD Athlon II X2 220 Socket AM3 (938) @ 2.1GHz 6GB RAM Firefox 26.0
_________________________________________________________________________________________

I'm fighting against patent trolls. Join me and tell your representative to support the #SHIELDAct: https://eff.org/r.b6JJ /via @EFF

My DC page: http://kyrathaba.dcmembers.com | My blog: http://williambryanmiller.com/ | Proofreading Service: http://bit.ly/1fQSqQP

Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.057s | Server load: 0.08 ]