[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

file browser in vc++

Posted on 2003-02-25
8
Medium Priority
?
399 Views
Last Modified: 2007-12-19
how to add file browser button(code) on GUI,that will allow me to select a particular file in a particular directory.
0
Comment
Question by:kaushik787
8 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 80 total points
ID: 8016640
If you're using MFC, you can use the CFileDialog class to help find the file.  Like:

CFileDialog fd(TRUE);
fd.m_ofn.lpstrFilter = "Text files\0*.txt\0All Files\0*.*\0"; // Use NULLS to separate the filters
// from the text to be displayed
if(fd.DoModal() == IDOK) // Display the dialog
{
  CString file_name = fd.GetPathName();
  MessageBox(file_name);
}
0
 

Author Comment

by:kaushik787
ID: 8022832
i need a detailed description, i'm designing a GUI for first time.Is there any built in control to do this job.
0
 

Author Comment

by:kaushik787
ID: 8023005
hi mnashadka it's helping,the only problem now is that how do i give the path in edit box which is suppose to show file,instead of showing it on message box.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Expert Comment

by:anjalidevi
ID: 8023952
Hi kaushik787 ,

Try this


-Anjali

#include <windows.h>
#include <commdlg.h>

GetFileName( char* sFName);

main()
{
char sFName[100];
/* the "sFname "variable will have the name of the file selected
*/

/*Call the function*/
GetFileName(sFName);
/*use sFname to get the selected file*/

}


bool GetFileName( char* sFName)
{

     TCHAR szFilename[MAX_PATH] = TEXT("");

     TCHAR FlExtn[MAX_PATH] = TEXT("all Files");

    BOOL bResult = FALSE;
    DWORD dwError = NOERROR;
    OPENFILENAME ofn = {0};

    ofn.lStructSize          = sizeof (OPENFILENAME);
     ofn.lpstrFilter          = TEXT("all Files(*.*)\0*.*\0\0");
    ofn.lpstrFile          = szFilename;
    ofn.nMaxFile          = MAX_PATH;
     ofn.lpstrInitialDir = NULL;
     ofn.lpstrDefExt          = FlExtn;
     
     ofn.Flags = OFN_EXPLORER |
                    OFN_ENABLEHOOK |
                    OFN_FILEMUSTEXIST|
                    OFN_EXTENSIONDIFFERENT|
                    OFN_PATHMUSTEXIST;
     ofn.lpstrTitle = "Open";
     bResult = GetOpenFileName(&ofn);
     


    if (bResult == FALSE)
    {
        dwError = CommDlgExtendedError ();
        return false;
    }
     
          strcpy(sFName,szFilename);
          return true;
}

0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8024219
If you have an edit box, the easiest way to show the path is to assign a variable to the edit box.  If you have a CString named m_filename that is assigned to the edit box (using the ClassWizard or Add Variable in the ClassView if you're using the .NET version of the compiler), it might look like:

CFileDialog fd(TRUE);
fd.m_ofn.lpstrFilter = "Text files\0*.txt\0All Files\0*.*\0"; // Use NULLS to separate the filters
// from the text to be displayed
if(fd.DoModal() == IDOK) // Display the dialog
{
 m_filename = fd.GetPathName(); // Assign the variable to the result
 UpdateData(FALSE); // Update the data in the windows
}
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8024238
If it's in a dialog box, you could also use SetWindowText.  If the control id of the edit box was IDC_FILENAME, it would be:
CFileDialog fd(TRUE);
fd.m_ofn.lpstrFilter = "Text files\0*.txt\0All Files\0*.*\0"; // Use NULLS to separate the filters
// from the text to be displayed
if(fd.DoModal() == IDOK) // Display the dialog
{
  GetDlgItem(IDC_FILENAME)->SetWindowText(fd.GetPathName());
}
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8024341
If you make the edit box multiline, then you can read the file and do a SetWindowText or update a variable with the contents using ifstream or CFile.  Would you rather be more Microsoft-specific or standard?  My e-mail is mike.nashadka@sanmina-sci.com
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9845985
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to mnashadka: Grade A

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question