Forum > General
Selecting, not opening, multiple files so as to use path/file names
stephanos14:
Dear All
I have written a playlist programme for my mp3 player in Lazarus 2.0.8. It writes to an M3U file on the player and the programme is on the player as well. When the programme starts it reads the M3U file and places the content into a memo:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Memo1.Lines.LoadFromFile('PlayListStephen.m3u');
I have been using a TfileNameEdit window (and the OnChange event), to navigate to an MP3 file and select it. Then append it to the memo content:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TheFileName := FileNameEdit1.Text; // a variable := the path/file name menuString := Memo1.Lines.Text; // a variable := the memo text menuString := Concat(menuString, TheFileName); // concat the two variables// Now write new content of menuString back into the Play List Window (Memo.Lines.Text)
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- Memo1.Lines.Text := menuString;
This works fine for one mp3 file selection. But I want to expand the programme to select multiple files. To this end I have set “OfAllowMultuSelect” of TfileNameEdit, to true. I can now select multiple files from within the Open window. When two or more files are selected their names appear in the input window (.Text property). However, the File Name input of the Open dialogue box shows the two files I selected, each contained in double quotes with a space between them. Like this: [ "SimpleForms.res" "SimpleForms.exe" ]. After pressing the Open button, I have coded to place the content of the Text property input to a variable:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TheFileName := FileNameEdit1.Text;and then use it as a caption for a label, just to make sure I have both file names:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- Label10.Caption := TheFileName;The output/Caption is:
C:\Users\stephanos\MyFiles\Delphi(Lazarus)\PlayListV11\SimpleForms.exe
But I do not have both file names. Only the file name of the first selected file, and its full path. The full path is needed as are subsequent file names.
What is the best way to achieve this objective?
The answer appears to be use the onAcceptFileName event and SynEdit (SynMemo being deprecated). I added to “uses”:
SynEdit, SynEditKeyCmds;
but got this error message at compile:
“Compile Project, Target: SimpleForms.exe: Exit code 1, Errors: 1
simpleformsunit.pas(8,3) Fatal: Cannot find SynEdit used by SimpleFormsUnit.
Check if package SynEdit is in the dependencies of the Project Inspector.”
I found the Project Inspector but cannot progress as it is all new to me. I looked around but cannot find any clear instructions. I am using Windows 10 and Lazarus 2.0.8.
Any help appreciated and I also recognise this is a big ask
jamie:
apparently the
FileNameEdit1.DialogFiles.Text contains the list after the dialog closes with full path for each entry
stephanos14:
Dear jamie
Thanks. But the reality is it does not or it does not when I want to use the variable for the caption of a label.
There is much I do not know. Can you assist further?
jamie:
Yes, it appears that control is a little buggy..
The OnAcceptFileName comes in before the DialogFiles gets populated.
Here is an work around with code to show you how to scroll through the list.
This is just a part you need to drop into an app, add the FIleNameEditIndex in the form ..
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FileNameEdit1Change(Sender: TObject);begin If Not FileNameEdit1.Modified Then Begin Memo1.Text := FileNameEdit1.DialogFiles.Text; end; FileNameEdit1.Modified := false;end; procedure TForm1.FileNameEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin if FileNameEdit1.DialogFiles.Count <> 0 THen Begin If Key =VK_up Then Begin FileNameEditIndex := (FIleNameEditIndex-1) mod FileNameEdit1.DialogFiles.Count; if FIleNameEditIndex < 0 Then FileNameEditIndex := FileNameEdit1.DialogFiles.Count-1; End else If key=VK_Down Then FileNameEditIndex := (FileNameEditIndex+1) mod FileNameEdit1.DialogFiles.Count; FileNameEdit1.Caption := FileNameEdit1.DialogFiles[FileNameEditIndex]; FileNameEDit1.Modified := True; end;end;
jamie:
I found the missed up location in the source file
if you want to fix it you can but do as you please. Maybe I should report this...
Looks like someone was having fun, must likely more of this in there.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFileNameEdit.SaveDialogResult(AKind: TDialogKind; D: TCommonDialog);var FN: String;begin case AKind of dkOpen, dkPictureOpen : begin FilterIndex := TOpenDialog(D).FilterIndex; FN := TOpenDialog(D).FileName; if (FN <> '') then begin if Assigned(OnAcceptFileName) then OnAcceptFileName(Self, FN); end; if (FN <> '') then begin // set FDialogFiles first since assigning of FileName trigger events FDialogFiles.Text := TOpenDialog(D).Files.Text; FileName := FN; end; end; dkSave, dkPictureSave : begin FileName := TSaveDialog(D).FileName; FilterIndex := TSaveDialog(D).FilterIndex; FDialogFiles.Clear; end; end;end;
Examine that and tell me what you fine that looks wrong. Also take note of the comment left there. %)
Navigation
[0] Message Index
[#] Next page