Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to show the list of files of specific type(say music files) in List box how to do this?

when the list is displayed in the list box and i select any one item it should open the file how to make it?
Posted
Updated 24-Feb-12 0:32am
v2

There are a lot of code examples you can find with google.
http://www.hassan-ahmad.com/2011/03/c-list-all-the-files-in-directory-recursively/[^]

Good luck!
 
Share this answer
 
Comments
chaiein 23-Feb-12 4:21am    
Thank you but i need to display the contents in listBox.
Jochen Arndt 23-Feb-12 4:34am    
Replace the output line by calling AddString(strPath) of your list box.
chaiein 24-Feb-12 6:03am    
ya this is working for me.Thanks:)
chaiein 24-Feb-12 6:30am    
when i click on the list of items i should be able to open that file how to do?
See here[^] for an easy solution from Microsoft.
 
Share this answer
 
Comments
chaiein 24-Feb-12 5:36am    
Its not working
DlgDirList("C:\",IDC_LIST_SONGS,0,DDL_READWRITE);

It gives a break as access voilation
Richard MacCutchan 24-Feb-12 5:43am    
It's not working
That does not tell me anything. Edit your original question, show the code that you have created and give the exact text of any error messages. An access violation suggests a bug in your code.
chaiein 24-Feb-12 6:29am    
I got the list of files path in the listbox using the following link
http://www.hassan-ahmad.com/2011/03/c-list-all-the-files-in-directory-recursively/
void CDemoDlg::GetDirList(CString sPath)
{
	WIN32_FIND_DATA FindFileData;
	sTmpPath = sPath;
	sTmpPath += "\\*.*";
	HANDLE hFind = FindFirstFile( sTmpPath, &FindFileData );
	if ( hFind == INVALID_HANDLE_VALUE )
        return;
    else {
		do {
		if ( ( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
		 {
		// if directory:
		if ( strcmp(".", FindFileData.cFileName ) && strcmp("..", FindFileData.cFileName) ) 
		{
		sTmpPath = sPath;
		sTmpPath += "\\";
		sTmpPath += FindFileData.cFileName;
		GetDirList( sTmpPath);
		}
	} 
	else // if file:
	{
	sTmpPath = sPath;
	sTmpPath += "\\";
	sTmpPath += FindFileData.cFileName;

	m_SongsList.AddString(sTmpPath);/* adding path to the list Box*/
					
            }
        } while ( FindNextFile( hFind, &FindFileData) != 0 );
			  FindClose( hFind );
    }
    return;
}




C#
void CDemoDlg::OnLbnSelchangeListSongs()
{
    int sel;
    CString Song;
    sel=m_SongsList.GetCurSel();
    m_SongsList.GetText(sel,Song);
    PlaySound(Song,NULL, SND_FILENAME|SND_ASYNC);/* Play only .wav form music files*/

}
 
Share this answer
 
v2
Comments
Jochen Arndt 24-Feb-12 7:13am    
You found the answer while I was writing mine. Fine.
Solution for the updated question "When the list is displayed in the list box and i select any one item it should open the file how to make it?"

The best option would be adding an 'Open' button to the window containing the list box or using the OK button if the the list box resides in a dialog. From within the button handler (OnOK() with the default OK button), get the selected file name(s) from the list box and proceed. When using the OK button, the file name(s) can be stored in a member variable to be acessed by the code that shows the dialog.

Get the selected file name for a single selection list box:
C++
void CMyDialog::OnOK()
{
    int nSel = m_list.GetCurSel();
    if (nSel < 0) // no file name selected
        return;
    m_list.GetText(nSel, m_strFileName);
    // Calling function will open file
    CDialog::OnOK();
}
void CMyDialog::OnBnClickedOpen()
{
    int nSel = m_list.GetCurSel();
    if (nSel < 0) // no file name selected
        return;
    CString strFileName;
    GetText(nSel, strFileName);
    // Open file here
    OpenFile(strFileName);
}


With a multi selection list box, use GetSelItems() and store the names in a CStringArray.

You may also use a LVN_ITEMCHANGED handler which checks for changed selection state. But this would trigger with every change of the selection (when moving through the list with cursor keys) which is not useful when opening a file at each change.
 
Share this answer
 
Comments
chaiein 25-Feb-12 6:16am    
OpenFile(strFileName); //here error as does not take one argument

when i gave OpenFile(songPath,OFS_MAXPATHNAME,OF_READWRITE); also 2nd argument error
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365430(v=vs.85).aspx



Instead to play .Wav file i have a solution that is

PlaySound(filename.wav,NULL, SND_FILENAME|SND_ASYNC);
Jochen Arndt 25-Feb-12 6:29am    
I used OpenFile() as a placeholder for some kind of function to open a file.
Hi Y'all

I also had problems using DlgDirList and searched for 2 days on the solution.
I came across this site from MSDN

http://msdn.microsoft.com/en-us/library/windows/desktop/hh298372(v=vs.85).aspx[^]

and I saw that in the InitDialog part of the code they used DlgDirList.
So I copied this part of the code ofcourse with variable declarations into my own program :

void CTwoFormsDlg::FillList()
{
PTSTR pszCurDir;
TCHAR achBuffer[MAX_PATH];
pszCurDir = (PTSTR)spathOnly.GetString();
SetCurrentDirectory(pszCurDir); // First Set the directory to path
pszCurDir = achBuffer;
GetCurrentDirectory(MAX_PATH, pszCurDir); // Then get the directory path
DlgDirList(pszCurDir, CL_SECLIST, 0, 0);}

Removed SetFocus because this did not work and after all that searching I had no intention to go and look for that again so that you have to figure out yourself.
But the sub works when I press a button that has a call to that function.
Hope this helps.

Changed some things in the code where spathOnly is declared in MyAppDlg.h , CL_SECLIST is de name of the list control.

Greetz

Richard
 
Share this answer
 
v2
Comments
CHill60 17-Jun-14 12:31pm    
Same link was posted 2.5 years ago
Member 15511520 20-Feb-22 23:30pm    
How to get the filesname in a folder to a listcontrol?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900