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.
eyemagAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
VincentLawlorConnect With a Mentor Commented:
Place a filelist and list box on your form

Make the filelist hidden

For i = 1 to File1.ListCount

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

Next i

There you go a list with no file extensions.

Vin.
0
 
kpcapelCommented:
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"
0
 
eyemagAuthor Commented:
vin-

filelist1 does NOT have an Item property, so please provide the adequate prop and ill give you the points
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
VincentLawlorCommented:
Sory it should read File1.List(i)

Vin.
0
 
DennisBorgCommented:
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
0
 
KDivadCommented:
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
0
 
DennisBorgCommented:
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
0
 
ArkCommented:
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
0
 
KDivadCommented:
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>)
0
 
ArkCommented:
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
0
 
VincentLawlorCommented:
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.

0
 
KDivadCommented:
NDavid? Me? What parsing?

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

Parsing comment was meant for DennisBorg.

InStrRev was meant for you.

Vin.
0
 
KDivadCommented:
Ah, ok, I was getting a bit confused there. Wondered if I made a post that I couldn't remember...
0
 
VincentLawlorCommented:
No you're not loosing your mind :-)
0
 
DennisBorgCommented:
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
0
 
KDivadCommented:
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.
0
 
DennisBorgCommented:
>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
0
 
KDivadCommented:
<<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...
0
 
KDivadCommented:
<<... 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
0
 
VincentLawlorCommented:
That's why InstrRev will work.

Vin.
0
 
ArkCommented:
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 :)
0
 
VincentLawlorCommented:
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.
0
 
VincentLawlorCommented:
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.
0
 
KDivadCommented:
<<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.
0
 
ArkCommented:
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
0
 
VincentLawlorCommented:
Thanks.

Vin.
0
 
eyemagAuthor Commented:
thanks

sorry for the delay...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.