Thread: OpenFile Dialog Box

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Exclamation OpenFile Dialog Box

    I am trying to write an app that will allow the user to open up a file and load it into my program. The file that will be loaded will probably be just a standard .txt file.

    I would like to use the Windows standard OpenFile Dialog box, but I am having trouble getting the box to open.

    I create a OPENFILENAME testfile, and then pass that to the function GetOpenFileName(&testfile); The window is never displayed, and I am out of ideas. If somebody could please help me that would be great. Thanks in advance.

    - Brice

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Have you initialised the members of the OPENFILENAME struct?
    zen

  3. #3
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    Here is one way to do it.
    Code:
    //Common control dialog
    CString csFile; 
    CString name=("default") //Default text file
    CString path=("C:\\windows\\desktop\\"+name);  //Default directory
    
    //The actual meat of the code
    //Use TRUE to open FALSE to save
    CFileDialog theFileDialog(TRUE,"TXT",path,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,"txt files(*.txt)|*.txt||");
         if(theFileDialog.DoModal()==IDOK)
         {
              csFile= theFileDialog.GetPathName();
              ifstream saveFile(csFile);
         }
    Last edited by ski6ski; 09-30-2001 at 05:38 AM.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    16
    I have tried to initialize the struct, but I am not sure if I have everything in there that I need. Here is my struct

    test.hwndOwner = NULL;
    test.lpstrFilter = "All Files (*.*)\0*.*\0";
    test.lpstrCustomFilter = NULL;
    test.nMaxCustFilter = NULL;
    test.nFilterIndex = 1;
    test.nMaxFile = 256;
    test.lpstrFileTitle = NULL;
    test.nMaxFileTitle = NULL;
    test.lpstrInitialDir = NULL;
    test.lpstrTitle = NULL;
    test.Flags = OFN_LONGNAMES;

    after that I call GetOpenFileName(&test) and I never see my dialog box. Did I forget to initialize something??

    - Brice

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You have to initialise the lStructSize member with the size of the struct -

    test.lStructSize=sizeof(OPENFILENAME);
    zen

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Talking

    Thanks zen!! I didnt realized that it needed that sizeof(struct) it works now. Would I do the same thing for a Save As dialog box? I am assuming so

    - Brice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. Parent of a BrowseForFolder dialog box
    By @nthony in forum Windows Programming
    Replies: 4
    Last Post: 01-08-2007, 02:54 PM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Dialog Box & Property Sheet :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 01:33 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM