Click to See Complete Forum and Search --> : How do I drag files from a list box and open them in a text box on the same form?


Shannon
January 26th, 1999, 12:27 AM
O.k. here is the problem that I need help on. I have one form, and on this form I have list box and a text box. What I wanna do is I want the list box to list the files of what ever folder I might have open. which I know how to do that, but what I wanna do is I want to be able to drag the files from the list box into the text box and have them open in that text box. is there anyway to do that? I mean like if i have the list box programmed to show text files, can I drag one of those text files from the list box and have it open in the text box on the same form? Thanks for any help anybody can give me.


Shannon

Chris Eastwood
January 26th, 1999, 03:12 AM
Hi


Here's a quick and dirty method (there are plenty of other ways of doing it also)


Take a form, add a textbox (text1) and a listbox (list1) and set the following properties on the textbox


MultiLine = True

ScrollBars = Both


Now paste the following code into the form :


Option Explicit


Private Sub Form_Load()

Dim lCount As Long

Dim sTmpStr As String



sTmpStr = Dir$("c:\*.bat")



With List1

.OLEDragMode = 1 ' automatic

End With



With Text1

.OLEDropMode = 1 ' manual

End With

'

' Add all the files from the specified directory to the listbox

'

Do While Len(sTmpStr) > 0

List1.AddItem sTmpStr

sTmpStr = Dir

Loop



End Sub


Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim lFileNum As Long

Dim sTmpStr As String

Dim sBuffer As String



lFileNum = FreeFile



Open "c:\" & Data.GetData(vbCFText) For Input As lFileNum

Do While Not EOF(lFileNum)

Line Input #lFileNum, sBuffer

sTmpStr = sTmpStr & vbCrLf & sBuffer

Loop

Close lFileNum



Text1.Text = sTmpStr



End Sub


Regards


Chris Eastwood

Software Engineer

ACNielsen Ltd


CodeGuru - the website for developers

http://www.codeguru.com