Link to home
Start Free TrialLog in
Avatar of eyemag
eyemag

asked on

filelistbox

hey

need to hide file extensions in filelistbox.

if unachievable, please provide code to load a normal listbox with a specific group of files from a given directory which have a *.txt extension.
Avatar of kpcapel
kpcapel

If you'd like to show only files with a given extension, the FileListBox allows for that.  Just set the Pattern property.

FileListBox1.Pattern = "*.txt"
ASKER CERTIFIED SOLUTION
Avatar of VincentLawlor
VincentLawlor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eyemag

ASKER

vin-

filelist1 does NOT have an Item property, so please provide the adequate prop and ill give you the points
Sory it should read File1.List(i)

Vin.
You do not have to use a hidden FileList Box, especially since you are not using it except to obtain the list of files. This would eat up more resources than is necessary.

An alternative would be to use the following subroutine, which can be added to a Standard Code Module:

Public Sub LoadTextFileList(ByVal DirName As String, lb As ListBox)
   Dim strFile As String

   If Right(DirName,1) <> "\" Then DirName = DirName & "\"
   DirName = DirName & "*.txt"
   With lb
      .Clear
      strFile = Dir(DirName)
      Do While Len(strFile)
         .AddItem strFile
         strFile = Dir()
      Loop
   End With
End Sub



To populate a listbox with the list of files, you would use code similiar to the following:

   LoadTextFileList "C:\MyFiles", List1


-Dennis Borg
List1.Add Mid(FileList1.Item(i), 1, Instr(FileList1.Item(i), ".")-1)

Careful. What will that return on a filename like this?:

Dr. Jones.txt


FileListBox1.Pattern = "*.txt"

That will also return any names like this:

*.txtw
*.txt3
One thing I did forget in my proposed solution, was to suppress the file extension. To accomplish this, the "AddItem" line of code should be modified as follows:

   .AddItem Left(strFile, Len(strFile) - 4)

Here is the revised code, in its entirety:

Public Sub LoadTextFileList(ByVal DirName As String, lb As ListBox)
  Dim strFile As String

  If Right(DirName,1) <> "\" Then DirName = DirName & "\"
  DirName = DirName & "*.txt"
  With lb
     .Clear
     strFile = Dir(DirName)
     Do While Len(strFile)
        .AddItem Left(strFile, Len(strFile) - 4)
        strFile = Dir()
     Loop
  End With
End Sub
Avatar of Ark
Hi

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const LB_DIR = &H18D
Private Const LB_RESETCONTENT = &H184
Public Enum DDL_FLAGS
   DDL_READWRITE = &H0
   DDL_READONLY = &H1
   DDL_HIDDEN = &H2
   DDL_SYSTEM = &H4
   DDL_DIRECTORY = &H10
   DDL_ARCHIVE = &H20
   DDL_DRIVES = &H4000
   DDL_EXCLUSIVE = &H8000
End Enum

'Return number of files added
Public Function ListBoxToFileList(lb As ListBox, ByVal InitDir As String, Optional wFlags As DDL_FLAGS = 0, Optional sExt As String = "*.*", Optional bShowExt As Boolean = True) As Long
    Dim LastIndex As Long
    Dim s As String
    Call SendMessage(lb.hwnd, LB_RESETCONTENT, 0, ByVal 0)
    If Right(InitDir, 1) <> "\" Then InitDir = InitDir & "\"
    LastIndex = SendMessage(lb.hwnd, LB_DIR, wFlags, ByVal InitDir & sExt)
    If bShowExt = False Then
       For i = 0 To IastIndex
           s = lb.List(i)
           s = Left(s, Len(s) - Len(sExt) + 1)
           lb.List(i) = s
       Next i
    End If
    ListBoxToFileList = nItems + 1
End Function


'Calls:
'add listbox and command button
Private Sub Command1_Click()
  Caption = ListBoxToFileList(List1, "c:\windows", DDL_HIDDEN + DDL_ARCHIVE + DDL_READONLY + DDL_SYSTEM, "*.exe", False) & " files added."
End Sub


Cheers
Ark,
Without actually trying that code, did you just turn a listbox into a filelistbox???? Cool! Very cool! A filelistbox with all the ability of a listbox! Very, very, VERY cool! Great job!

(Too much enthusiam? <grin>)
Thanks :)
You also can easy modify above code with following cons:
Private Const CB_DIR = &H145
Private Const CB_RESETCONTENT = &H14B
to make same things with combobox

BTW, if you need really <COOL> FileListBox, check my recent addition to FreeVBCode:
http://www.freevbcode.com/ShowCode.Asp?ID=2977

Cheers
To NDavid your Parsing will not work for files with extensions like .html or .rs

I should have used InstrRev(FileList1.List(i), ".")-1

Vin.

NDavid? Me? What parsing?

InStrRev is VB6 only. eye never specified a version or I would have suggested it.
Sorry KDivad a little Dyslectic in my spelling ko.

Parsing comment was meant for DennisBorg.

InStrRev was meant for you.

Vin.
Ah, ok, I was getting a bit confused there. Wondered if I made a post that I couldn't remember...
No you're not loosing your mind :-)
VincentLawlor:


>Parsing comment was meant for DennisBorg.

>your Parsing will not work for files with extensions like .html or .rs

Good point, but this was done by design (i.e. on purpose), since eyemag specifically mentioned s/he was targetting only TXT files.

-Dennis Borg
Still wouldn't have worked completely. If you hunt using the pattern "*.txt", it will actually return files that match the pattern "*.txt*". It'd be fairly rare, but it will still happen.
>Still wouldn't have worked completely. If you hunt using
>the pattern "*.txt", it will actually return
>files that match the pattern "*.txt*". It'd be fairly
>rare, but it will still happen.

No offense, but I had to try that one out myself.

It is not *exactly* the same as "*.txt*", but is similiar. It returned the following files:

   F1.txt
   F1.txtA
   F1.txtABC

but it did not return:

   F1.txt.jpg  <---- *.txt*  would return this file also


I consider this to be a bug on Microsoft's part, for "*.txt" should not return files such as F1.txtA

I wonder if the Win API FindFirstFile() and FindNextFile() also exhibit the same behavior.


-Dennis Borg
<<No offense, but I had to try that one out myself.>>

None taken. I would have had to try it myself.

<<F1.txt.jpg  <---- *.txt*  would return this file also>>

Odd. Hadn't tried it on a doubled extension. It could be that if you have a . in the pattern, it assumes an extension is being looked for. In that case, "*.txt*" does work properly. (Still haven't tested on a doubled extension. Just guessing)

<<I wonder if the Win API FindFirstFile() and FindNextFile() also exhibit the same behavior.>>

I've never used them, so I'm not going to try figuring them out now...
<<... an extension is being looked for.>>

As such, the first part of a doubled extension isn't actually an extension, but part of the file's name as in:

Dr. Jones.txt
That's why InstrRev will work.

Vin.
Private Declare Function PathFindExtention Lib "Shell32" Alias "#31" (ByVal sPath As String) As Long
Private Declare Function CopyStringA Lib "kernel32" Alias "lstrcpyA" (ByVal NewString As String, ByVal OldString As Long) As Long

Private Sub Command1_Click()
   Dim sPath As String, sExt As String
   sPath = "c:\windows\calc.exe"
   sExt = String(260, 0)
   Call CopyStringA(sExt, PathFindExtention(sPath))
   sExt = Left(sExt, InStr(1, sExt, Chr(0)) - 1)
   Debug.Print sExt
End Sub

Enjoy :)
Ark,

Tried your Control from above but it keeps giving me errors on load. It's complaining about license information. I have registered the OCX. Any ideas ??

Vin.
eyemag

>>vin-

filelist1 does NOT have an Item property, so please provide the adequate prop and ill give you the points

Have you forsaken us..

Vin.
<<That's why InstrRev will work.>>

Right. I was just finishing explaining my post.

I have a bad habit of finishing a conversation after everyone else is done... <grin>


<<Tried your Control from above but it keeps giving me errors on load. It's complaining about license information. I have registered the OCX. Any ideas ??>>

That means Ark included a license requirement when he compiled the ocx. Either he'll have to give you the license or he'll have to remove the license and recompile.
Hi
No license, there are source code. Unfortunatelly, MS Common Controls versions have no backward compatibility. Do following:
Open source code project. When VB say smth about mscomctrl.ocx, press continue. Check all user controls. Common controls in them will be replaced with picture boxes. Remove these pictureboxes. Then add common controls to user control:
FolderTree require treeview, Foldercombo-ImageCombo and FileList - ListView. Last two controls require imagelist with one dummy 16x16 icon. Common controls can be any size with default names and properties.
Recompile project.

Enjoy

Cheers
Thanks.

Vin.
Avatar of eyemag

ASKER

thanks

sorry for the delay...