Solved

FileListBox

Posted on 2000-05-08
9
458 Views
Last Modified: 2010-05-18
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
Comment
Question by:ote
  • 6
  • 2
9 Comments
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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
 
LVL 9

Expert Comment

by:Ruchi
Comment Utility
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
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
also note you dont need a filelistbox on your form at all with this method
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:ote
Comment Utility
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
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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
 

Author Comment

by:ote
Comment Utility
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
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
Comment Utility
I assume this answered your question
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

743 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

19 Experts available now in Live!

Get 1:1 Help Now