Download presentation
Presentation is loading. Please wait.
Published byGeraldine Gray Modified over 9 years ago
1
Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs
2
Programs that Use Files Some programs use exactly the same file each time –Usually an initialization file in the same place every time Most programs can use any file of the proper format Typing the file name into a text box is awkward and error prone Let Windows show us where it is with a common dialog box Copyright © 2004-2013 - Curt Hill
3
Common File Dialog Box What is it? –This is the Windows standard way to select a file name –There are two flavors: open existing or create new one Where: Dialogs tab There are two of importance today: –wxOpenFileDialog and wxSaveFileDialog Neither will open a file –But they will obtain a file name Copyright © 2004-2013 - Curt Hill
4
What you can do A common dialog box allows the user to avoid typing and Change directory or disk Choose a file Change the filter Accept the chosen file or cancel Copyright © 2004-2013 - Curt Hill
5
Example from PowerPoint Copyright © 2004-2013 - Curt Hill
6
General Unlike most components they are non-visual They can be seen on the form during design time Invisible at execution time They are not seen until executed Copyright © 2004-2013 - Curt Hill
7
DevC++ Design Time Copyright © 2004-2013 - Curt Hill
8
After Drop Copyright © 2004-2013 - Curt Hill
9
Using Once put on form we still have to use it General process is: Display box Determine if open or cancel was done If Open get the file name Convert this file to a C style string pass it to the file Copyright © 2004-2013 - Curt Hill
10
Modal Dialog Boxes In Windows there are two styles of dialog box: –Modal –Non-modal A non-modal may be displayed, but it does not interfere with the parent window A Modal dialog box prevents any input to the parent –It must be dismissed before the parent is usable Copyright © 2004-2013 - Curt Hill
11
ShowModal Method The ShowModal method takes no parameters and shows the dialog ShowModal returns an integer that tells what happened If this is equal to wxID_OK then the user approved the file name –Otherwise they cancelled –All file processing should be done only if a wxID_OK was returned Copyright © 2004-2013 - Curt Hill
12
Design Properties Message – The contents of the title bar when Common dialog is displayed Name – The name of the component –This will always be referenced from code Most other things need to be referenced using methods Copyright © 2004-2013 - Curt Hill
13
GetPath Method This is used to obtain the filename –It also contains the entire directory path –GetFilename only gives the file name without the path It returns a wxString ifstreams and ofstreams need a C style string This needs to be converted in order to pass to a constructor or open method How? Copyright © 2004-2013 - Curt Hill
14
String Conversion This is relatively easy Use the wxString method: c_str() –ifstream inf( OpenDialog1-> GetPath ().c_str()); You may do in two steps: –wxString name = OpenDialog->GetPath(); –ifstream inf(name.c_str()); Copyright © 2004-2013 - Curt Hill
15
Sample Code Suppose that OpenDial is the object The following code will open a file: int ret = OpenDialog- >ShowModal(); if(ret == wxID_OK){ ifstream inf( OpenDial->GetPath().c_str()); while(inf) { … } } // end of if Copyright © 2004-2013 - Curt Hill
16
Executed Copyright © 2004-2013 - Curt Hill
17
Filters By default a common dialog box will show every file in the current directory –The directory may be changed Often we are not interested in every kind of file –A filter allows us to only observe the files of interest DevC++ does not allow the filter to be set at design time –Done with SetWildcard method Copyright © 2004-2013 - Curt Hill
18
Filter Pairs Each pair consists of two pieces –A name to be displayed –An expression that describes what to show The two are separated by a vertical bar The expression may include ? or * –? any one character –* any number of any characters Copyright © 2004-2013 - Curt Hill
19
Format Thus we set: OpenDialog1->SetWildcard( "Data|*.dat|All|*.* "); The odd entries are display The even entries are the actual filters See next screen for image Copyright © 2004-2013 - Curt Hill
20
Show Filters Copyright © 2004-2013 - Curt Hill
21
Last Thoughts There are many other options –Most of these we do not need The FileName is persistent It will show as the last one used in subsequent executions Copyright © 2004-2013 - Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.