Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

FileListBox

In a VB4 programm there is a FileListBox,
named File1.
This FileListBox shows the files that exist in a certain directory.
The files are: Readme.txt, Win.zip, Foto.jpg
How could be the names of these files to be shown  in the FileListBox
with uppercase letters and without extension?
Like this: README, WIN, FOTO.
0
ote
Asked:
ote
  • 6
  • 2
1 Solution
 
AzraSoundCommented:
i dont think you will want to use a filelistbox in this case, but rather, a true list box in which you can manipulate the appearance the way you want to
0
 
RuchiCommented:
Place a filelistbox and a listbox on the form. Add the following code to the form. This is how you can hide the extensions and use uppercase letters.



Option Explicit

Private Function LastInStr(ByVal String1 As String, ByVal String2 As String, Optional ByVal Start As Long = 0, Optional Compare As VbCompareMethod = vbBinaryCompare) As Long
    LastInStr = Start
    While InStr(LastInStr + 1, String1, String2, Compare)
        LastInStr = InStr(LastInStr + 1, String1, String2, Compare)
    Wend
End Function

Private Sub CopyLists(lstBoxDest As ListBox, filListBoxSrc As FileListBox)
    Dim I As Integer, S As String
    Call lstBoxDest.Clear
    For I = 0 To filListBoxSrc.ListCount - 1
        S = filListBoxSrc.List(I)
        S = UCase$(Left(S, LastInStr(S, ".") - 1))
        Call lstBoxDest.AddItem(S)
    Next
End Sub

Private Sub File1_PathChange()
    Call CopyLists(List1, File1)
End Sub

Private Sub File1_PatternChange()
    Call CopyLists(List1, File1)
End Sub

Private Sub Form_Load()
    Dim I As Long
    File1.Visible = False
    Call CopyLists(List1, File1)
End Sub
0
 
AzraSoundCommented:
i assume you have a dirlistbox working in conjunction here...in that case it can all be done in one routine:


Private Sub Dir1_Change()
    Dim filesys, f, files, fileObj As Object
    Dim path, tempStr As String
    Dim pos As Long
    List1.Clear
    path = Dir1.path
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set f = filesys.GetFolder(path)
    Set files = f.files
    For Each fileObj In files
        pos = InStr(1, fileObj.Name, ".")
        tempStr = Left(fileObj.Name, pos - 1)
        tempStr = UCase(tempStr)
        List1.AddItem tempStr
    Next
End Sub


Now the listbox acts just like the filelistbox but the way you want it to

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
AzraSoundCommented:
also note you dont need a filelistbox on your form at all with this method
0
 
oteAuthor Commented:
A comment to AsraSound

I think your  2nd answer could help me.

The FileListBox in my programm, contains
pictures files (.jpg) which by the next lines in:

Private Sub File1_Click()

f$ = File1.Path + "\" + File1.filename
Image1.Picture = LoadPicture(f$)

are shown as image in the form.

With the  "Private Sub Dir1_Change()"
you sent me, the names of picture files are shown
indeed with uppercase letters and without extensions exactly as I like.

The problem that I have now is how can I select
the files from the ListBox.
Thank you
0
 
AzraSoundCommented:
create an array to hold the "true" filenames...for example:



Dim arr As Variant
Dim i As Integer

Private Sub Dir1_Change()
    ReDim arr(0) As Variant
    i = 0
    Dim filesys, f, files, fileObj As Object
    Dim path, tempStr As String
    Dim pos As Long
    List1.Clear
    path = Dir1.path
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set f = filesys.GetFolder(path)
    Set files = f.files
    For Each fileObj In files
        pos = InStr(1, fileObj.Name, ".")
        tempStr = Left(fileObj.Name, pos - 1)
        tempStr = UCase(tempStr)
        List1.AddItem tempStr
        arr(i) = fileObj.Name
        ReDim Preserve arr(i + 1)
        i = i + 1
    Next
End Sub

Private Sub List1_Click()
    Dim item As Integer
    item = List1.ListIndex
    MsgBox arr(item)
End Sub


now everytime you click on the listbox a msgbox will pop up showing the actual filename...you can alter it a bit to suit your needs (i.e. to open the file)

0
 
oteAuthor Commented:
What I am looking for is:

1) I have some picture-files (.jpg) existing in the folder c:\icons\  
2) In the form there is an ImageControl
3) The names of the picture-files are shown in a ListBox.

How could I present the pictures in the ImageControl by a click on their names in ListBox?

Thanks
 
0
 
AzraSoundCommented:
ok using the method i pointed to earlier...you should be able to use

Dir1.Path & arr(item) for the full path


Now you can load it as follows,


Private Sub List1_Click()
    Dim item As Integer
    item = List1.ListIndex
    Set Image1.Picture = LoadPicture(Dir1.Path & arr(item))
End Sub




0
 
AzraSoundCommented:
I assume this answered your question
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now