Recent

Author Topic: Enabled (and visible) not the same with BitBtn and FileNameEdit  (Read 3947 times)

jamie

  • Hero Member
  • *****
  • Posts: 7308
Re: Enabled (and visible) not the same with BitBtn and FileNameEdit
« Reply #15 on: August 16, 2025, 01:48:43 am »
That is still going to get you in trouble!

 FILES property is a TSTRINGS type which can hold multiple lines, each being separated by a LF (Line Feed) for which will get attached to the end of your string.
 
  Even if you only selected one item in the list, there exists the #11 ($0A) character at the end.

 To get the currently selected name, use "FileName" property or, you can index the FILES property once you know one has been selected.

 
Code: Pascal  [Select][+][-]
  1.    If AOpenDialog.Files.Count <> 0 then
  2.    MyLabel.Caption := AOpenDialog.Files[0];
  3.  

Jamie
The only true wisdom is knowing you know nothing

cdbc

  • Hero Member
  • *****
  • Posts: 2464
    • http://www.cdbc.dk
Re: Enabled (and visible) not the same with BitBtn and FileNameEdit
« Reply #16 on: August 16, 2025, 07:45:34 am »
Hi
You could try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if OpenDialog1.Execute then begin
  4.     Edit1.Text:= OpenDialog1.Filename;
  5.     Button1.Enabled:= true;
  6.     ... // further processing of filename if needed// ...
  7.   end else Edit1.Text:= 'User canceled...';
  8. end;
  9.  
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99

wp

  • Hero Member
  • *****
  • Posts: 13210
Re: Enabled (and visible) not the same with BitBtn and FileNameEdit
« Reply #17 on: August 16, 2025, 11:31:24 am »
Code: Pascal  [Select][+][-]
  1. uses
  2.  Dialogs;
  3.  
  4. procedure TForm1.BitBtn2Click(Sender: TObject);  // new append files
  5. var  //
  6.   OpenDialogA: TOpenDialog;
  7.   TheFileNames : string;
  8. begin
  9.      OpenDialogA := TOpenDialog.Create(self);
  10.   OpenDialogA.Execute;
  11.     TheFileNames := OpenDialogA.Files.Text;
  12.   Label3.Caption := TheFileNames;
  13. end;  
A few comments on this:

First: This code is in the handler of the OnClick event of BitBtn2. Whenever you click on this button an instance of TOpenDialog will be created. As a rule of thumb, every "create" should be accompanied by a corresponding "Free" - this is not found here. Strictly speaking this does not produce a "real" memory leak because the OpenDialogA is created with "self" (i.e. the form) as "Owner". This means that the form will destroy the OpenDialog at the end. But nevertheless during execution of your application the available memory will be reduced with every click on the button --> Call OpenDialogA.Free at the end of the code. And then you can pass "nil" as owner of the dialog which is cheaper. Use a try-finally block to make sure that the Free is executed even when an exception is raised somewhere after Create.

Second: The TOpenDialog, by default, has Multiselection deactivated. Since I know from your previous posts, that your application needs multiselection capabilities you must add the option "ofMultiSelect" to the Options of the OpenDialog in order to activate it.

Third: OpenDialogA.Execute is a function. The return value is true when the dialog has been closed with the OK button. Depending on the result of the Execute function the event handler usually decides whether the following code should be envoked or not. You don't check the return value of Execute. Therefore, the Label3.Caption will always display the filenames, even when the user cancelled the dialog.

Third: A more convenient way to store the selected file names than in a TLabel is to use a TListView or TCombobox where every filename is a single item accessible in an array-like manner. In a TLabel you have all filenames in a long string and you must write extra code to extract a specific filename.

Code: Pascal  [Select][+][-]
  1. uses
  2.  Dialogs;
  3.  
  4. procedure TForm1.BitBtn2Click(Sender: TObject);  // new append files
  5. var  
  6.   OpenDialogA: TOpenDialog;
  7. begin
  8.   OpenDialogA := TOpenDialog.Create(nil);
  9.   try
  10.     OpenDialogA.Options := OpenDialogA.Options + [ofMultiSelect];
  11.     if OpenDialogA.Execute then
  12.       Listbox1.Items.Assign(OpenDialogA.Files);
  13.   finally
  14.     OpenDialogA.Free;
  15.   end;
  16. end;

Another rule of thumb: Always be critical when you find code snippets in the internet. And be particularly critical when code is proposed by so-called artivicial "intelligence".

« Last Edit: August 16, 2025, 11:33:44 am by wp »

jamie

  • Hero Member
  • *****
  • Posts: 7308
Re: Enabled (and visible) not the same with BitBtn and FileNameEdit
« Reply #18 on: August 16, 2025, 07:36:46 pm »
Yes I 100% agree with @WP, and don't take any wooden nickels

Jamie
The only true wisdom is knowing you know nothing

stephanos

  • Jr. Member
  • **
  • Posts: 79
Re: Enabled (and visible) not the same with BitBtn and FileNameEdit
« Reply #19 on: August 18, 2025, 12:47:36 pm »
Dear All

Thanks for the additional feedback.

I have now implemented “OpenDialogA.Free;” at the end of the process.

I was aware of the #10 char at the end of the string which was stopping the target file I wanted to open being found.  I had the problem in the Windows and Linux versions.  With the single file selection, I remove #10 by way of
Code: Pascal  [Select][+][-]
  1. Tag := Pos(#10, Localm3uPathFile, 1); // find the position of #10
  2. Localm3uPathFile := copy(Localm3uPathFile, 1, (Tag-1));
and that allows me to open the file:
Code: Pascal  [Select][+][-]
  1. Memo1.Lines.LoadFromFile(Localm3uPathFile);

One button uses TopenDialog; to select and open a m3u file and another uses TopenDialog; to select multiple files.  The coding for the latter is:
Code: Pascal  [Select][+][-]
  1. OpenDialogA := TOpenDialog.Create(self);
  2. OpenDialogA.Options := [ofAllowMultiSelect];
  3. OpenDialogA.Execute;
  4. TheFileNames := OpenDialogA.Files.Text; // see note A
  5. OpenDialogA.Free;
                                 
Code: Pascal  [Select][+][-]
  1. // Note A – TheFileNames is a string which contains the path/filenames of all the selected files and includes a new line character between each path/file

All comments in your recent posts welcomed and much for me to read

 

TinyPortal © 2005-2018