MFC CFileDialog question

This is a discussion on MFC CFileDialog question within the Windows Programming forums, part of the Platform Specific Boards category; Yes I'm finally trying to do some MFC programming and finding out that...it's really not that difficult. So I'm smacking ...

  1. #1
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586

    MFC CFileDialog question

    Yes I'm finally trying to do some MFC programming and finding out that...it's really not that difficult. So I'm smacking myself for not having tried it earlier.

    I'm having one problem however with CFileDialog. I'm using the OFN_ALLOWMULTISELECT option in my common dialog box. I'd like to then retrieve all the selected file names from the dialog box.

    According to MFC docs all you need to do is this:

    1. Call POSITION GetStartPosition() to get the starting position
    2. Call GetNextPathName(POSITION &pos) to get the next file name.

    My questions are:

    Does pos auto-increment inside of CFileDialog or do I have to increment it. For some reason my dialog is crashing miserably when I attempt to iterate through the list.

    Code:
    ...
    CFileDialog *AddFile=new CFileDialog(true,"*.bmp",NULL,OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT,"BMP files (*.bmp)|*.bmp|");
      int result=AddFile->DoModal();
      
      //If user pressed ok or dialog succeeded
      if (result==IDOK)
      {
        
        //Get start position
        POSITION pos=AddFile->GetStartPosition();
    
        //Setup CString for what user picked
        CString UserChoice;
    
        //Get path from dialog
        UserChoice=AddFile->GetNextPathName(pos);
       
        //Setup success/fail variable
        bool bFailed=false; 
    
        //CString for comparison if path already in list
        CString AlreadyInList;
    
        while (UserChoice) 
        {
        
          //Iterate through list box items amd check to see if path
          //already in list
          for (int i=0;i<m_lbFileList.GetCount();i++)
          {
            m_lbFileList.GetText(i,AlreadyInList);
            if (UserChoice.Compare(AlreadyInList)==0)
            {
              UserChoice+=" is already in the list.";
              ::MessageBox(0,UserChoice,0,0);
              bFailed=true;
                  
            } 
          }
         
         //If path is not in list...add it to list box here
         if (!bFailed) 
         {
            m_lbFileList.AddString(UserChoice);
            bFailed=false;
         }  
    
          //pos++   ?????????????
          UserChoice=AddFile->GetNextPathName(pos);
        }
      }
      
       //Cleanup
       if (AddFile) delete AddFile;
    ...
    I still hate GUI programming but it is becoming necessary to create nifty tools and editors for my games.

    Any help would be greatly appreciated.
    Last edited by VirtualAce; 01-29-2005 at 09:57 PM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Which line is the problem?

    Does it handle one file successfully?
    Have you looked at the returned data?

    CFileDialog::GetOFN() (ie display lpstrFile in a messagebox, should contain the path, then each filename)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by Bubba
    Yes I'm finally trying to do some MFC programming
    It's about time! It's really a piece of cake, especially after all that graphics programming you like to tinker with.

    Code:
    ...
    CFileDialog *AddFile=new CFileDialog(true,"*.bmp",NULL,OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT,"BMP files (*.bmp)|*.bmp|");
      int result=AddFile->DoModal();
      
      //If user pressed ok or dialog succeeded
      if (result==IDOK)
      {
        
        //Get start position
        POSITION pos=AddFile->GetStartPosition();
    
        //Setup CString for what user picked
        CString UserChoice;
    // moved 
        //Setup success/fail variable
        bool bFailed=false; 
    
        //CString for comparison if path already in list
        CString AlreadyInList;
    
        while (pos != null) 
        {
          //Get path from dialog
          UserChoice=AddFile->GetNextPathName(pos);
    
          //Iterate through list box items amd check to see if path
          //already in list
          for (int i=0;i<m_lbFileList.GetCount();i++)
          {
            m_lbFileList.GetText(i,AlreadyInList);
            if (UserChoice.Compare(AlreadyInList)==0)
            {
              UserChoice+=" is already in the list.";
              ::MessageBox(0,UserChoice,0,0);
              bFailed=true;
                  
            } 
          }
         
         //If path is not in list...add it to list box here
         if (!bFailed) 
         {
            m_lbFileList.AddString(UserChoice);
            bFailed=false;
         }  
    //removed
        }
      }
      
       //Cleanup
       if (AddFile) delete AddFile;
    ...

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Aha thanks. So pos is NULL instead of CString. I figured it was something to do with that. Why don't the docs just say.....pos returns null at the end of the list?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC simple question
    By dole.doug in forum Windows Programming
    Replies: 5
    Last Post: 09-26-2008, 06:11 AM
  2. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  3. MFC UI question
    By naruto in forum Windows Programming
    Replies: 1
    Last Post: 04-21-2005, 10:10 PM
  4. Question under MFC...
    By NewbieStats in forum C++ Programming
    Replies: 0
    Last Post: 03-30-2005, 02:24 PM
  5. MFC Dialog App Screen Refresh Question
    By Scotth10 in forum Windows Programming
    Replies: 2
    Last Post: 10-18-2002, 02:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21