tags:

views:

118

answers:

2
+2  Q: 

file selection

In vc++ 6.0,MFC i want to select a multiple files

   CFileDialog opendialog(true); // opens the dialog for open;;<br>
    opendialog.m_ofn.lpstrTitle="SELECT FILE"; //selects the file title;;<br>
    opendialog.m_ofn.lpstrFilter="text files (*.txt)\0*.txt\0"; //selects the filter;;<br>

    if(opendialog.DoModal()==IDOK) //checks wether ok or cancel button is pressed;;
    {
        srcfilename=opendialog.GetPathName(); //gets the path name;;
        --------------
         --------------
    }

The sample of code above it allows only single file can be select at a time,but i want select a multiple text files eg : holding control key (ctrl+select the multiple files). so any body give the solution thanks in advance.

+1  A: 

You should pass the OFN_ALLOWMULTISELECT flag in OpenFileName structure to allow the multi selection.

Naveen
please can you me a example so that i will get more clear ,thank you
+4  A: 

So in the constructor for CFileDialog you can set the dwFlags parameter to have 'OFN_ALLOWMULTISELECT'. Thats the easy part, to actually get the multiple file names back you have to modify the m_ofn.lpstrFile member in the CFileDialog to point to a buffer that you have allocated. Have a look here:

http://msdn.microsoft.com/en-us/library/wh5hz49d(VS.80).aspx

Here is an example use of it, hope the comments suffice:

void CMainFrame::OnFileOpen()
{
    char strFilter[] = { "Rule Profile (*.txt)|*.txt*||" };

    CFileDialog FileDlg(TRUE, "txt", NULL, OFN_ALLOWMULTISELECT, strFilter);
    CString str;
    int nMaxFiles = 256;
    int nBufferSz = nMaxFiles*256 + 1;
    FileDlg.GetOFN().lpstrFile = str.GetBuffer(nBufferSz);
    if( FileDlg.DoModal() == IDOK )
    {
     // The resulting string should contain first the file path:
     int pos = str.Find(' ', 0);
     if ( pos == -1 );
      //error here
     CString FilePath = str.Left(pos);
     // Each file name is seperated by a space (old style dialog), by a NULL character (explorer dialog)
     while ( (pos = str.Find(' ', pos)) != -1 )
     { // Do stuff with strings
     }
    }
    else
     return; 
}
DeusAduro
still i am confusion , i am not getting clearly, please will u give me sample of code or any example,or please modify my code given above thank you