|
|
Home » Community » Newbie corner » using a file chooser (dir chooser??) to get a dir and not a file
using a file chooser (dir chooser??) to get a dir and not a file [message #32742] |
Sun, 05 June 2011 11:41 |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
How do you choose a directory rather than a file?
I have been using this code to choose a file:
FileSel fn;
void OpenAction() {
fn.AllFilesType();
String filename = fn;
fn <<= Nvl(r_d, recent_dir);
if(fn.ExecuteOpen()){ // file chooser
workdirField<<=Value(fn);
r_d = fn;
recent_dir = ~fn;
}
// work.Run(THISBACK(Work));
}
How can this be done to select a directory?
Is all I want to do is display the path and use that like:
cd C:\0-Neil\FAA\2011\Sectionals
I could parse the String filename but that would require a user to pick a file in that directory rather than just pick a directory. In some apps a directory can be chosen instead of a file.
Is there something built into U++ that can just select a directory??
[Updated on: Sun, 05 June 2011 13:20] Report message to a moderator
|
|
|
|
Re: using a file chooser (dir chooser??) to get a dir and not a file [message #32749 is a reply to message #32745] |
Mon, 06 June 2011 03:24 |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
Thanks Honza
I saw that but it gives no example code of how that is used.
Ecxuse me but I am not sharp enough to decipher this into usable code:
Quote: | bool ExecuteSelectDir(const char *title = NULL)
Executes directory selection dialog. title can specify alternate caption of dialog. If user selects a file to open, returns true.
|
It looks like it returns a bool of true only if a specified ("file name") exists and not actually choosing a directory
I did a search in the forum for "ExecuteSelectDir" and found threads like this:
http://www.ultimatepp.org/forum/index.php?t=msg&goto=314 13&&srch=ExecuteSelectDir#msg_31413
which had posts that included code like this:
FileSel fs;
fs.ActiveDir("C:/Test");
if (!fs.executeselectdir("Ouvrir à partir de ..."))
return;
String selectedFolder = ~fs;
Trying code similar this there were errors like "it doesn't take 0 parameters".
After searching and trying for several hours I posted for help.
In Java it can be done with this"
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Do you have a U++ code example or a link to code I could try?
[Updated on: Mon, 06 June 2011 05:48] Report message to a moderator
|
|
|
Re: using a file chooser (dir chooser??) to get a dir and not a file [message #32752 is a reply to message #32749] |
Mon, 06 June 2011 08:17 |
|
The usage of ExecuteSelectDir() is very similar to ExecuteOpen(). Following code is taken from examples/CompDir, I just added few comments.
void DlgCompareDir::DoBrowse(Ctrl *field) // field is EditString or similar Ctrl
{
FileSel fsel;
fsel.AllFilesType();
static String recent_dir; // variable to store last opened directory
fsel <<= Nvl((String)~*field, recent_dir); //before opening, we pre-set the dialog to the value in field, fallback to recent_dir if there is Null
if(fsel.ExecuteSelectDir()) //open the dialog to select directory
*field <<= recent_dir = ~fsel; // assign the selected directory 'recent_dir' and also fill it into 'field' Ctrl
}
BTW: I just found there is even simpler way to do this, although not as versatile:String mydir = SelectDirectory();
Honza
|
|
|
|
|
Goto Forum:
Current Time: Wed Jun 05 08:25:09 CEST 2024
Total time taken to generate the page: 0.02052 seconds
|
|
|