Selecting (Choosing) Folders and listing files to a listbox

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
nassausky
Posts: 21
Joined: Wed Nov 18, 2009 4:41 pm

Selecting (Choosing) Folders and listing files to a listbox

Post by nassausky »

Does anyone have a small piece of code for selecting a folder using a GetFolder GUI and sending the file listing in that folder to a listbox?
_________________
Sincerely,

Mike
nassausky
Posts: 21
Joined: Wed Nov 18, 2009 4:41 pm

Re: Selecting (Choosing) Folders and listing files to a list

Post by nassausky »

OK I think I got it. I kept trying to do it with
aadd or afill but both those methods didn't work. I left here the small bit of code I used.

Code: Select all

*ChooseInput is a text control which got it's value from the GetFolder function
 
local aDir := Directory(Main.ChooseInput.Value+"\*.*")
local x

For x:=1 to len(aDir)
  if x = 1
   Main.afiles.Item(1):=adir[x,1]
  else  
   Main.afiles.AddItem(adir[x,1])
  end if   
Next

*Made an edit to the code and added an IF condition because the first item in the listbox was always blank for some reason. Who knows. If anyone knows why it wouldn't work without the manually adding an entry into the first array position give me a shout.
_________________
Sincerely,

Mike
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Selecting (Choosing) Folders and listing files to a list

Post by esgici »

Hi Mike

Since you did'nt give a complete (SSW) code, sadly I don't understood your problem(s) :(

But this prg works well:

Code: Select all

#include <hmg.ch>

PROCEDURE MAIN()

   DEFINE WINDOW frmTestDirList;
      AT 0, 0;
      WIDTH  553 ;
      HEIGHT 554 ;
      TITLE "Test DIR List" ;
      MAIN ; 
      ON INIT FillDirList()
      
      @ 10,  100 BUTTON  btnReLoad    CAPTION "Re-Load" ACTION FillDirList() 
      @ 100, 100 LISTBOX lstbxDirList WIDTH 200 HEIGHT 200       
      
   END WINDOW // frmTestDirList
   
   frmTestDirList.Center
   frmTestDirList.Activate
      
RETURN // MAIN()

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PROCEDURE FillDirList()

LOCAL cDIRName   := GetFolder( "Choose a folder" ) 
LOCAL aDirList   := {}
LOCAL nDirElemNo :=0

IF !EMPTY( cDIRName ) 

   aDirList := Directory( cDIRName )
   IF EMPTY( aDirList )
      MsgInfo( cDIRName + CRLF + " This folder doesn't contains ordinary file")
   ELSE
      
      frmTestDirList.lstbxDirList.DeleteAllItems()
   
      FOR nDirElemNo := 1 to LEN( aDirList )
         frmTestDirList.lstbxDirList.AddItem( aDirList[ nDirElemNo, 1 ] )  
      NEXT
   ENDIF EMPTY( aDirList )
      
ENDIF !EMPTY( cDIRName ) 

RETURN // FillDirList()

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I hope that it help you.

Regards

--

Esgici
Viva INTERNATIONAL HMG :D
nassausky
Posts: 21
Joined: Wed Nov 18, 2009 4:41 pm

Re: Selecting (Choosing) Folders and listing files to a list

Post by nassausky »

Excellent, yes thanks, I just figured it out before you posted it.

I am curious though how come you added this line

Code: Select all

frmTestDirList.lstbxDirList.DeleteAllItems()
Why does the listbox create an empty item on initialization?

That's not important but I figured I would ask.

Thanks again!
_________________
Sincerely,

Mike
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Selecting (Choosing) Folders and listing files to a list

Post by Rathinagiri »

If you don't delete the items in the listbox, every time when you click the button, the items are added last to the listbox.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Selecting (Choosing) Folders and listing files to a list

Post by esgici »

nassausky wrote:... I figured I would ask.
You are right, it's unnecessary on initialization, required when call by button.

But it's also unnecessary testing calling sequence ( called by ON INIT or BUTTON ?) and seperating process ( emty or not) within sub-procedure.

It isn't ?

Regards

--

Esgici
Viva INTERNATIONAL HMG :D
Post Reply