How to select more than one file at a time via a Dialog Box ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to select more than one file at a time via a Dialog Box ?

Ken Lee
Friends,

I would like to allow the user to select one or more text files at the
same time, and then allow my application to operate on that collection
of files.

Right now, I use the following code to open a single file - but this
method does not allow the user to select more than one file at a time
(via CTRL or SHIFT).

fileName := FileOpenDialog new
                         caption: 'Choose the File';
                          value:  'e:\XXX\xxx.txt';
                          fileTypes: #( ('Text Files (*.xxx)'
'*.xxx') );
                          showModal.

How can I get a collection of file names ?

As always, thanks so much for your help and insights !

Best wishes,

Ken Lee


Reply | Threaded
Open this post in threaded view
|

Re: How to select more than one file at a time via a Dialog Box ?

Ian Bartholomew
Ken,
    Try the following. You use it in the same way as a FileOpenDialog but it
allows multiple selections and answers either nil or a collection of full
(i.e. each one has a complete path) filenames.

    The buffer size is hard coded into it but I've been using this since the
early betas and it hasn't caused a problem for me. It should allow around
100 files before failing - if you need more than that just increase the
buffer size.

Ian

FileOpenDialog subclass: #MultipleFileOpenDialog
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    classInstanceVariableNames: ''

MultipleFileOpenDialog>>showModal
    "Show a Open File dialog for the receiver.
    Answer a String containing the name of the selected file or nil if no
selection was made.
    Modified for MultipleFiles
        Bigger buffer
        Parse buffer for zero terminated strings stopping at double 0
        Answer nil, array with one file (windows adds path) or array with
lots of files (we add path)"

    | buf filename result files |
    buf := String new: 10000.

    filename := self value.
    filename notNil ifTrue: [buf replaceFrom: 1 to: filename size with:
filename].
    self winStruct
        nMaxFile: buf size;
        lpstrFile: buf;
        flags: self style.

        result := ComDlgLibrary default perform: self commonDialogSelector
with: winStruct.
        result ifTrue: [
            buf := buf copyFrom: 1 to: (buf indexOfSubCollection: ##(String
with: Character null with: Character null)).

        self value: (buf subStrings: Character null).
        self apply ].

    files := self answer.
    (files isNil or: [files size <= 1]) ifTrue: [^files].
    ^(2 to: files size) collect: [:index | File composePath: (files at: 1)
subPath: (files at: index)]

MultipleFileOpenDialog>>style
    "Answers the style for the receiver"
    ^super style bitOr: (OFN_ALLOWMULTISELECT | OFN_EXPLORER)


Reply | Threaded
Open this post in threaded view
|

Re: How to select more than one file at a time via a Dialog Box ?

Ken Lee
Ian -

Works like a charm.

You're the man !

Best wishes,

Ken Lee

>     Try the following. You use it in the same way as a FileOpenDialog
but it
> allows multiple selections and answers either nil or a collection of
full
> (i.e. each one has a complete path) filenames.
>
>     The buffer size is hard coded into it but I've been using this
since the
> early betas and it hasn't caused a problem for me. It should allow
around
> 100 files before failing - if you need more than that just increase
the

> buffer size.
>
> Ian
>
> FileOpenDialog subclass: #MultipleFileOpenDialog
>     instanceVariableNames: ''
>     classVariableNames: ''
>     poolDictionaries: ''
>     classInstanceVariableNames: ''
>
> MultipleFileOpenDialog>>showModal
>     "Show a Open File dialog for the receiver.
>     Answer a String containing the name of the selected file or nil if
no
> selection was made.
>     Modified for MultipleFiles
>         Bigger buffer
>         Parse buffer for zero terminated strings stopping at double 0
>         Answer nil, array with one file (windows adds path) or array
with
> lots of files (we add path)"
>
>     | buf filename result files |
>     buf := String new: 10000.
>
>     filename := self value.
>     filename notNil ifTrue: [buf replaceFrom: 1 to: filename size
with:
> filename].
>     self winStruct
>         nMaxFile: buf size;
>         lpstrFile: buf;
>         flags: self style.
>
>         result := ComDlgLibrary default perform: self
commonDialogSelector
> with: winStruct.
>         result ifTrue: [
>             buf := buf copyFrom: 1 to: (buf indexOfSubCollection:
##(String
> with: Character null with: Character null)).
>
>         self value: (buf subStrings: Character null).
>         self apply ].
>
>     files := self answer.
>     (files isNil or: [files size <= 1]) ifTrue: [^files].
>     ^(2 to: files size) collect: [:index | File composePath: (files
at: 1)

> subPath: (files at: index)]
>
> MultipleFileOpenDialog>>style
>     "Answers the style for the receiver"
>     ^super style bitOr: (OFN_ALLOWMULTISELECT | OFN_EXPLORER)
>
>
>
>
>
>