Wednesday, December 31, 2008

drag’n drop files into richtext boxes (so it’s actually useful)

There’s a ton of howto’s out there showing people how to do the drag and drop into a richtext box.  But recently I had a job that required me not to drag the contents from one part of the form to the other but to actually drag a file (or files) into a RichTextbox control – and process them.

Which… if you’ve never tried this before drops the file as an icon you can click on and open.  Not really imageuseful if you’re trying to process files.  It’s kind of annoying that even after a bit of Googling you’ll get a lot of links that let you drag the text or images or all kinds of stuff from one part of the form – to another part of the form. 

Which is fine if that’s what you need.  But what I needed was to be able to grab several files – of either text or xml – and then process them and display the output. 

Since I can’t share my work code – here’s the training wheels version of the code for you. image As you can see we process the XML and display it.  It also does text so … this is fairly close to the final version – which hopefully I’ll take some time and cleanse it from any confidential use info that may be in it and share.  In the mean time – here’s the actual guts of the code that makes it work.

 

Since I’m reallllly running behind – that’s all I can get out this week  - but hopefully it’ll be of use to someone out there.  As you can see – I’m calling the richtextbox dragenter event and customizing it.  The first thing I do is hunt for the dataformats.filedrop type off the event handleer – this allows me to grab the filename and – load it appropriately into the richtextbox.

Anyway… to all out there – here’s the code – have an AWESOME New Year… and I’ll be back on line to annoy all as soon as I can.

 

Imports System
Imports System.Drawing
Imports System.Data
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Xml
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'This call is required by the Windows Form Designer.
RichTextBox1.AllowDrop = True
End Sub

Private Sub
richTextBox1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles RichTextBox1.DragEnter

Try
If
e.Data.GetDataPresent(DataFormats.FileDrop) Then
' Put all file names into a string array
'in case the user grabbed more than one file.
Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
Try
'Now we break it down - we only want text files, xml or xsd files
'so we trap for that if it's not one of those - we ignore it.
If files(0).Contains(".txt") Then
Dim
f_stream As IO.StreamReader = FileIO.FileSystem.OpenTextFileReader(files(0))
RichTextBox1.Text = f_stream.ReadToEnd()
'Just for fun we show the name of the file and link it
llblFileName.Text = files(0).ToString

f_stream.Close()
ElseIf files(0).Contains(".xml") Then
Dim
filename As String = files(0)
Dim reader As New XmlTextReader(files(0))
llblFileName.Text = files(0).ToString
reader.MoveToContent()
'Loop through the XML and display it in the richtextbox
Do While reader.Read
RichTextBox1.AppendText(reader.ReadContentAsString)
Loop
reader.Close()
End If
Catch
ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
End If
Catch
ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub
llblFileName_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llblFileName.LinkClicked
System.Diagnostics.Process.Start(llblFileName.Text)
End Sub
End Class

No comments: