[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Add drag and drop to the markdown conversion program in C# and Microsoft Word

[Add drag and drop to the markdown conversion program in C# and Microsoft Word]

I've been working more with the example described at Convert docx files to md files with C# and Microsoft Word and I made a few improvements to make the program easier to use. The new version includes the following changes.

  • When a new item is added to the checked lost box, it is initially checked.
  • You can drag and drop files onto the program's executable to launch the program and add those files to the checked list box.
  • You can drag and drop files onto the program's checked list box to add those files to the list.


Intially Checking Items

The new version of the program adds files to its checked list box in several places, so it made sense to move the code that adds a file into a new method. The following AddFileToList method adds a file name to the list.

private void AddFileToList(string filename) { // See if the file is already in the list. foreach (FileData file_data in clbFiles.Items) { // If the file is in the list, do nothing. if (file_data.FileInfo.FullName == filename) return; } // Add the file to the list. clbFiles.Items.Add(new FileData(filename)); clbFiles.SetItemChecked(clbFiles.Items.Count - 1, true); }

This code loops through the list's current items. If an item's full file name matches the new file's name, the method exits without doing anything.

If none of the existing files match the new file, the method adds the new file to the list and checks it.

The program calls the AddFileToList method to add files from the File menu's Open command and from drag and drop, which is described next.


Drag Onto Executable

If you drag and drop a file onto the program's executable, it adds that file to the checked list box.

When you start a Windows Forms application by dragging files onto its executable, the program receives a command line argument list. The first argument is always the name of the executing program. The rest of the arguments are the file names.

The following Form_Load event handler examines the program's arguments and adds any file names to its list.

private void Form1_Load(object sender, EventArgs e) { string[] args = Environment.GetCommandLineArgs(); foreach (string filename in args) { if (!filename.ToLower().EndsWith(".exe")) AddFileToList(filename); } }

This code uses Environment.GetCommandLineArgs() to get the command line arguments and then loops through them.

The program can only process files that Word can open, so it skips any file name that ends with .exe. If the file's name does not end with .exe, the method calls AddFileToList to add it to the list.


Drag Onto List

The program's checked list box uses two event handlers to support drag and drop.

The following DragEnter event handler executes when you drag something over the list box.

private void clbFiles_DragEnter(object sender, DragEventArgs e) { // See if this is a copy and the data includes text. if (e.Data.GetDataPresent(DataFormats.FileDrop) && (e.AllowedEffect & DragDropEffects.Copy) != 0) { // Allow this. e.Effect = DragDropEffects.Copy; } else { // Don't allow any other drop. e.Effect = DragDropEffects.None; } }

This code gets the drag format to see if you are dragging files. If you are dragging files and the drag operation is Copy, then the method sets e.Effect to Copy to allow a copy operation. If you are not dragging files or the operation is not Copy, the code sets e.Effect to None to prohibit a drop. The control changes its drag cursor accordingly.

When you drop the files on the list, the following event handler executes.

private void clbFiles_DragDrop(object sender, DragEventArgs e) { string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string filename in filenames) AddFileToList(filename); }

This code uses e.Data.GetData to get the dropped files' names. It loops through the names and uses AddFileToList to add them to the list.


Conclusion

It's not too hard to add the ability to drag and drop items onto a program's executable or the controls it contains. Use the Form_Load event handler to drop things onto the executable. Use the DragEnter and DragDrop event handlers to drop things onto the program's controls. Use a common method (in this example AddFileToList) to make it easier to add files to the program from the File menu's Open command or from a drag and drop.

These changes make this example program much easier to use so I was able to convert about 100 files form docx format to md files in just a few minutes.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.