Solved

filelistbox

Posted on 2001-07-18
28
568 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:eyemag
  • 9
  • 8
  • 4
  • +3
28 Comments
 

Expert Comment

by:kpcapel
ID: 6294585
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
 
LVL 4

Accepted Solution

by:
VincentLawlor earned 50 total points
ID: 6294655
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
 

Author Comment

by:eyemag
ID: 6294898
vin-

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

Expert Comment

by:VincentLawlor
ID: 6294909
Sory it should read File1.List(i)

Vin.
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6295186
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
 
LVL 5

Expert Comment

by:KDivad
ID: 6295270
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
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6295403
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
 
LVL 27

Expert Comment

by:Ark
ID: 6297009
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
 
LVL 5

Expert Comment

by:KDivad
ID: 6297033
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
 
LVL 27

Expert Comment

by:Ark
ID: 6297060
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
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6297360
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
 
LVL 5

Expert Comment

by:KDivad
ID: 6298986
NDavid? Me? What parsing?

InStrRev is VB6 only. eye never specified a version or I would have suggested it.
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6299044
Sorry KDivad a little Dyslectic in my spelling ko.

Parsing comment was meant for DennisBorg.

InStrRev was meant for you.

Vin.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 6299054
Ah, ok, I was getting a bit confused there. Wondered if I made a post that I couldn't remember...
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6299106
No you're not loosing your mind :-)
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6299111
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
 
LVL 5

Expert Comment

by:KDivad
ID: 6299142
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
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6299186
>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
 
LVL 5

Expert Comment

by:KDivad
ID: 6299207
<<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
 
LVL 5

Expert Comment

by:KDivad
ID: 6299211
<<... 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
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6301364
That's why InstrRev will work.

Vin.
0
 
LVL 27

Expert Comment

by:Ark
ID: 6301593
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
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6301704
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
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6302555
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
 
LVL 5

Expert Comment

by:KDivad
ID: 6303274
<<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
 
LVL 27

Expert Comment

by:Ark
ID: 6304117
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
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6307572
Thanks.

Vin.
0
 

Author Comment

by:eyemag
ID: 6317604
thanks

sorry for the delay...
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now