RSS

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

31 Dec

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

Advertisements
 
Leave a comment

Posted by on December 31, 2008 in Design, Development, Internet, Work

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: