Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Drag and drop files into TX Text Control

This source code snippet requires TX Text Control ActiveX
Author:TX Text Control Support Department
Language:Visual Basic
Version:1.1
Released:October 02, 2002
Last modified:January 11, 2008
Requirements:TX Text Control ActiveX with Visual Basic
Download code:drag_drop_files.zip Download [3.71 KB, ZIP]

Drag and Drop is an often-used feature in windows environments. Dragging a file from the Explorer window and dropping it into Word is the easiest way to load a document. Here is how you can do that with TX Text Control, too.

It's bit harder to do than other Tips and Tricks we presented. That's because you must intercept a Windows message that the Explorer sends to the TX window. This process is called sub-classing.

The source code shows you how it works, but if you don't understand the whole thing it is strongly advised not to alter the code!

Furthermore, if sub-classing is active and the program causes an error or something similar then VB likes to crash. So save your code often.

The actual drop handling happens in the following handler:

  1. Private Sub OnDropFiles(Files() As String, Count As Integer, X As Long, Y As Long)
  2.  
  3. Dim iFileType As Integer, i As Integer
  4.  
  5. Form1.TXTextControl1.SelStart = Form1.TXTextControl1.InputPosFromPoint(X,Y)
  6.  
  7. ' If only one file is dropped, the user can choose how to insert the file
  8. If Count = 1 Then
  9. iFileType = GetFileType(Files(0))
  10.  
  11. If (iFileType = IMAGE_FILE) Then
  12. Form1.TXTextControl1.ObjectInsertAsChar 0, Files(0), -1, 100,
  13. 100, 0, 1, 0
  14. ElseIf (iFileType <> 0) Then
  15. ' Create a PopUp Menu
  16. Form1.PopupMenu Form1.mnuPopup, 0, X, Y
  17.  
  18. ' Either append documents or load the document...
  19. If (g_CurMenu = 1) Then
  20. Form1.TXTextControl1.Load Files(0), 0, iFileType, False
  21. ElseIf (g_CurMenu = 2) Then
  22. Form1.TXTextControl1.Load Files(0), 0, iFileType, True
  23. End If
  24. End If
  25. Else
  26. For i = 0 To Count - 1
  27. iFileType = GetFileType(Files(i))
  28. If (iFileType = IMAGE_FILE) Then
  29. Form1.TXTextControl1.ObjectInsertAsChar 0, Files(i), -1, 100,
  30. 100, 0, 1, 0
  31. ElseIf (iFileType <> 0) Then
  32. Form1.TXTextControl1.Load Files(i), 0, iFileType, True
  33. End If
  34. Next i
  35. End If
  36.  
  37. g_CurMenu = 0
  38.  
  39. End Sub

It is called when a WM_DROPFILES message is trapped and it capsulates the system calls, so you don't have to bother with these.

The first parameter of the OnDropFiles procedure is an array of strings. It holds the file names of all the files which have been dropped onto the TX. The Count parameter tells how many files were dropped and the X and Y parameters hold the coordinates of the drop event.

The first thing that happens in the handler is that the current input position of the TX window is set to the location were the drop event occured. This can be done using the InputPosFromPoint method.

It is then checked how many files are being dropped. If only one file is dropped we can offer the user the choice to insert the file the file being dropped into the existing text at the current input position or to load the file and replace the current contents of the document. A popup menu is shown to let the user decide.

But before we must determine the type of the file being dropped. This is done by the GetFileType function. It returns 0, if the file type is not known or the TX internal values for different file types. These are 1 for text, 5 for RTF and so on. If an image is dropped, the function returns 10 and not TX's Load function is called but the ObjectInsertAsChar method.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place