Results 1 to 12 of 12

Thread: listbox help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Posts
    98
    ok...i'm using this code to populate a list box with files from a certain directory:

    Code:
    Private Function Dir2(ByVal Path As String) As Variant
        'function for listing the skin names in the listbox
        Dim aList() As String
        Dim sDir As String
        Dim iIndex As Long
        If Right$(Path, 1) <> "\" Then Path = Path & "\"
        'List Directories Only
        sDir = Dir(Path, vbNormal)
        While Len(sDir)
            ReDim Preserve aList(iIndex)
            aList(iIndex) = sDir
            iIndex = iIndex + 1
            sDir = Dir
        Wend
        Dir2 = aList
    End Function
    
    Private Sub Form_Load()
        'start function for listing the skin names in the listbox
        Dim aDir As Variant
        Dim i As Long
        aDir = Dir2(frmMain.txtPath.Text)
        For i = 0 To UBound(aDir)
            frmMain.lstBitmaps.AddItem aDir(i)
        Next
        'remove the first 2 items:  "." and ".."
        frmMain.lstBitmaps.RemoveItem (0)
        frmMain.lstBitmaps.RemoveItem (0)
    End Sub
    is there anyway to make sure that only files with a certain extention show up in the box? thanks for your help.

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Try this:


    Dim strExistingFile As String
    strExistingFile = Dir(gstrProcessAreaDirectory & "*.jpg")
    Do Until strExistingFile = ""
    List1.Additem strExistingFile
    strExistingFile = Dir()
    Loop


    ______________________
    JPG FILES ONLY!
    Chemically Formulated As:
    Dr. Nitro

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Posts
    98
    nitro...this doesn't seem to be working. i changed the extention to "bmp" because that's what i needed and when i search a directory where i KNOW there are plenty bmp's nothing comes up. what could be wrong? also, do i have to define gstrProcessAreaDirectory anywhere? if so what as? is it a constant? it's not listed in the API text viewer. hmm. maybe i'm placing your snippet in the wrong place in my code. could you perhaps tell me what lines this is supposed to go in between in my original code that i posted? thanks a million for your help in the matter.

  4. #4
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Wink

    This seems to be the problem that alot of people have when using the Dir() function. Any time you try to find a file using it you must specify the file attribute bits to match in your search. When the function searches for the file it has to be one of these attributes, except if you specify vbNormal, then it will find files that have NO attributes set (the Archive bit is the exception). Therefore when you call the Dir function do it in the following manner to retrieve all files that you are looking for:
    Code:
    sBitmap = Dir(sDir & "*.bmp", vbNormal Or vbHidden Or vbReadOnly Or vbSystem)
    This will get you all the files you're looking for.

    Now as for your code, I don't usually do this but I noticed some problems in your code that are easily fixed.
    First of all:
    Code:
        'remove the first 2 items:  "." and ".."
        frmMain.lstBitmaps.RemoveItem (0)
        frmMain.lstBitmaps.RemoveItem (0)
    Should be the following:
    Code:
        'remove the first 2 items:  "." and ".."
        frmMain.lstBitmaps.RemoveItem (0)
        frmMain.lstBitmaps.RemoveItem (1)
    Next problem is a slighter bigger one. I can't stress this enough, but NEVER use variants! These memory behemoths are what lead to us VB programmers being mocked by are C++ bretheren. Just take a look in the VB documentation on the size requirements of Variants and you'll understand my plea. Yes, there are specific places where Variants are needed (eg. ParamArray), but if you know your data-type then don't use them unless absolutely necessary! If all you want to do is return an Array from your function then do by passing by reference, meaning that you declare an array in your calling procedure and pass it as an argument to the function you want to populate the Array. All actions you perform on the Array in the function will be reflected in the calling procedure's Array because of the nature of passing by reference, which coincidentally, Arrays are passed by as a rule in VB. With all that said you would modify your function's header in the following manner:
    Code:
    Private Sub Dir2(ByVal Path As String, ByRef aList() As String)
    Then in the calling procedure do the following:
    Code:
        'Dim aDir As Variant
        Dim aDir() As String
        Call Dir2(frmMain.txtPath.Text, aDir)

    There you go. Sorry to sound so condoning, it's just I'm sick of being mocked by C++ programs when I mention that my language of choice is VB. Even though most of the predujuices are no longer true, they can be if we don't optimize our code to VB's fullest potential.

    I'll get off the podium now, Bye.

    [Edited by SonGouki on 04-11-2000 at 10:31 PM]

  5. #5
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554
    Hi,

    I think (to songouki) that removing the first two items in the listbox was done correctly as once you remove item(0) then what was at item(1) is now become item(0)

    Hope this doesn't confuse matters

    DoxZaf
    {;->

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Posts
    98
    thank you very much songouki. your help is much appreciated. i apologize for the sloppy coding, but this is something i just put together for an experiment. so i didn't really pay attention to all that much detail. anyway, i agree with zaf kahn. it was done correctly because, as he said, listitem(1) becomes listitem(0) after executing the first piece of code. anyway, thanks a lot for the help. it's just what i needed. if there's any problems with the code you suggested i'll post another message. thanks again.

  7. #7
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Red face

    Whoops, boy is my face red! Sorry 'bout that. Happy to help with the other stuff though.

  8. #8
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    I haven't bothered to read the other replies completely but why don't you use a filelist box?
    Mark
    -------------------

  9. #9
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554

    Lightbulb I Agree With Mark Sreeves

    Use a FileListBox Control and set the PATH property to whatever your path ought to be and set the filter property to whatever the file extension ought to be.

    Unless off course you require something that the FileListBox does not allow/give you.

    DocZaf
    {;->

  10. #10
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Wink

    While that is true, guys, I always say that it's good to understand how the underlying code works (to what level of depth is your own decision). If he wants to write code to populate a list box with files then I say go for it, it is a very useful skill to know when the necessity for more complex file processing arises.

    Just my 2¢

  11. #11
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554
    I agree with you too SonGouki as having the underlying knowledge allows you to attack the problem more better as you have more weapons in your armoury.

    DocZaf
    {;->

    [Edited by Zaf Khan on 04-12-2000 at 08:39 PM]

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Posts
    98
    and this is exactly why i am doing this little experiment. i wanted to know everything there was to know about a list box and how/why to populate it. i am writing a program that uses listbox in a very limited way and i thought the other day that i wanted to learn more about why it does what it does and what other things i can use a listbox for. anyway, this happens a lot with me and probably too with most other programmers. i get an itch to know the in's and out's of a control or a function or an aspect of programming and i go for it. if i stumble along the way i usually come to this board. i have bever found such a wonderful resource of people willing to help out and teach others. it's simply fantastic. this has been like a gold mine to me ever since i found it. so with all that said, thanks to everyone who has ever helped me and the plenty others who have posted on this board. i hope you all realize what an asset you are to the programming community.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width