Solved

CFileDialog and Multiple File Types

Posted on 2008-06-17
7
913 Views
Last Modified: 2013-12-14
In my VC 6.0 app, I display a CFileDialog.  I simply want it to display all files with the following extensions, AT THE SAME TIME: ".txt", and ".xls".  I can get CFileDialog to display just the text files, or just the spreadsheet files, but I can't get it display both types at the same time.

Thanks, ahead of time.
0
Comment
Question by:clintonJ
7 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 21802469
You must use the following filter:

Text file (*.txt)|*.txt|Excel file (*.xls)|*.xls||
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 250 total points
ID: 21802477
Hi ClintonJ,

you just can seperate multiple extensions with a ;

I.e. use this filter string:

"Excel Files (*.txt;*.xls)|*.txt;*.xls|All files (*.*)|*.*||";


Hope that helps,

ZOPPO
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21802482
When you create it you can set the filter to be multiple types, using | as a separator, something like...

CFileDialog OpenDlg(false,NULL,NULL,0,_T("TXT Files (*.txt)|*.txt|XLS Files (*.xls)|*.xls"),NULL);
http://msdn.microsoft.com/en-us/library/wh5hz49d(VS.80).aspx

lpszFilter
A series of string pairs that specify filters you can apply to the file. If you specify file filters, only selected files will appear in the Files list box. See the Remarks section for more information on how to work with file filters
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Expert Comment

by:alb66
ID: 21802507
ZOPPO is right; I misunderstud the question.
0
 

Author Comment

by:clintonJ
ID: 21802571
OK, we're getting there.  The constructor for CFileDialog looks like this:

CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );

So, thanks to you, :) my lpszFilter parameter now looks like this:

 lpszFilter = "Data Files (*.txt;*.xls)|*.txt;*.xls|All files (*.*)|*.*||";

When the CFileDialog displays, the "Files of Type" textbox now displays the correct file extensions (.txt AND .xls).  

One small problem though: The only file names  that display are .txt files, even though I also have .xls files in the same folder.  Do I need to also adjust LPCTSTR lpszDefExt to get the excel file names to also display?

0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 250 total points
ID: 21802617
This works properli with VS2003:

      CFileDialog dlg( true, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
                                      "Data Files (*.txt;*.xls)|*.txt;*.xls|All files (*.*)|*.*||",
                                      NULL );
      dlg.DoModal();
0
 

Author Closing Comment

by:clintonJ
ID: 31467937
Thanks very much.  Perfecto!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now