Solved

File filter in managed Visual C++ .NET

Posted on 2004-08-22
10
526 Views
Last Modified: 2013-12-03
I’ve been trying to apply in managed VC++.NET the file filter which I use in VB.NET and which works fine there, supposing that in Visual Studio .NET those filters should not be language specific. Thus, in the Properties of the openFileDialog of a VC++.NET form, in the section Filter, I type in:

Document Files(*.txt)|*.txt

But it doesn’t seem to filter out the .txt files when I open the dialog.

Aslo, I’d like to have the extension .txt automatically attached to the file name when I type it in the saveFileDialog. How is all this done in managed Visual C++ .NET?
0
Comment
Question by:judico
  • 6
  • 3
10 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 250 total points
ID: 11867191
This will show only text files and add a .txt extension if you type in a new file name without an extension.  The SaveFileDialog is identical.

    System::Windows::Forms::OpenFileDialog *ofd = new System::Windows::Forms::OpenFileDialog();
    // Set up default extension
    ofd->AddExtension = true;
    ofd->DefaultExt = S"txt";
    ofd->CheckFileExists = false;

    // Set up filter
    ofd->Filter = S"Document Files (*.txt)|*.txt";

    if ( ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK )
    {
        System::Console::WriteLine(ofd->FileName);
    }
0
 

Author Comment

by:judico
ID: 11867402
Thank you so much for the prompt answer. I had to comment out the following, however:

if ( ofd->ShowDialog() == System::Windows::Forms::DialogResult::OK )
    {
        System::Console::WriteLine(ofd->FileName);
    }

Is there any specific reason why it should be there?  Also, I 'm still puzzled why wouldn't filtering work in managed VC++ .NET when set through the Properties of the form? I'm just curious and it has no bearing on your excellent answer. I wonder if I should ignore these Properties that accompany the forms and should learn to do everything through code?


P.S. I increased your points to 250. Please let me know if you have problems in receiving the increased points.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11867435
The 'if' statement is to test whether the dialog was cancelled or not - the test is true if the dialog was not cancelled.  The line inside the block was just a test line to print out the selected file name.

As for setting properties, I (almost) always code by hand, so I don't know why setting properties of the dialog would not work.  If I get some time I'll try it and see if it works.
0
 

Author Comment

by:judico
ID: 11867647
OK, thanks again ...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:judico
ID: 11870770
Since I don't see the ponts for answering this question increased I asked for increase in the Community Support:

http://www.experts-exchange.com/Community_Support/Q_21103692.html
0
 

Author Comment

by:judico
ID: 11870783
Just, out of curiosity, how can one have other file types listed in the drop-down menu of the openFileDialog?
0
 
LVL 19

Expert Comment

by:drichards
ID: 11870958
The filter is a concatenation of string pairs where each string is separated by '|'.  So to get multiple filter selections, just keep adding:

    ofd->Filter = S"Document Files (*.txt)|*.txt|Word Files (*.doc)|*.doc";

will give you two selections - "Document Files (*.txt)" and "Word Files (*.doc)".  You could keep going and tack on "|Excel Files (*.xls)|*.xls", etc.  You can also do multiple extensions at once by using ';' as a separator in the extension part of the pair:

    ofd->Filter = S"Document Files (*.txt)|*.txt|Word Files (*.doc,*.zip)|*.doc;*zip";
0
 

Author Comment

by:judico
ID: 11871876
Thanks a lot. I would appreciate it if you could take a look at:

http://www.experts-exchange.com/Hardware/Q_21102023.html

Thanks in advance.
0
 

Author Comment

by:judico
ID: 11877109
Netminder,

Thank you ... I just increased the number of points ... Please let me know if there are other problems.

judico
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 71
c++ syntax question 9 44
Add values of each row in an array 3 50
Path to  STL Map header file 1 43
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

896 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

14 Experts available now in Live!

Get 1:1 Help Now