Recent

Author Topic: Trouble with TFileOpen standard action  (Read 3998 times)

simsee

  • Full Member
  • ***
  • Posts: 230
Trouble with TFileOpen standard action
« on: October 02, 2016, 05:22:10 pm »
I'm new with Lazarus and I'm trying to understand actions (with some difficulties...).

I have ActionList1 : TactionList with a standard action FileOpen1 : TFileOpen.

Moreover I have Button1 : TButton on the main form, with its action property
set to FileOpen1 : TFileOpen.

In order to show the filename selected by the user in the dialog, I use the OnAccept
event of the action, with the following code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileOpen1Accept(Sender: TObject);
  2. begin
  3. ShowMessage(FileOpen1.FileName);
  4. end;

but I have an error by the compiler (identifier idents no member "filename").

Since FileOpen1 object is an instance of class TFileOpen, that inherits the property
FileName from TFileAction, why I can't use this property? And how can I know the filename selected
in the dialog?

Thanks id advance.
« Last Edit: October 02, 2016, 05:34:20 pm by simsee »

wp

  • Hero Member
  • *****
  • Posts: 13331
Re: Trouble with TFileOpen standard action
« Reply #1 on: October 02, 2016, 05:52:40 pm »
Look carefully at the object inspector: Filename is not a direct property of the TFileOpen action, but of its Dialog property. Therefore, you must call
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileOpen1Accept(Sender: TObject);
  2. begin
  3.   ShowMessage(FileOpen1.Dialog.FileName);
  4. end;

If you want to learn about Actions read Brian Long's article http://www.blong.com/Conferences/BorCon2003/Actions/6102.htm. It is about Delphi's actions, but everything can be applied to Lazarus as well.

simsee

  • Full Member
  • ***
  • Posts: 230
Re: Trouble with TFileOpen standard action
« Reply #2 on: October 02, 2016, 06:17:09 pm »
Thank you. Now my demo works. I missed this important detail.

I have read a more recert article by B. Long about actions on Delphi XE3,
but there are some differences with Lazarus...

wp

  • Hero Member
  • *****
  • Posts: 13331
Re: Trouble with TFileOpen standard action
« Reply #3 on: October 02, 2016, 06:20:30 pm »
Yes, that's why I referred to the older article. The newer article also mentions TActionManager and TActionBars which are not available in Lazarus.

simsee

  • Full Member
  • ***
  • Posts: 230
Re: Trouble with TFileOpen standard action
« Reply #4 on: October 02, 2016, 07:09:50 pm »
A final doubt: TFileOpen also inherits 'filename' protected property from TFileAction. So, why I can't write also: fileopen1.filename? I apologize for insisting with my trivial question...

(see: http://lazarus-ccr.sourceforge.net/docs/lcl/stdactns/tfileopen.html
http://lazarus-ccr.sourceforge.net/docs/lcl/stdactns/tfileaction.html)
« Last Edit: October 02, 2016, 07:32:12 pm by simsee »

wp

  • Hero Member
  • *****
  • Posts: 13331
Re: Trouble with TFileOpen standard action
« Reply #5 on: October 02, 2016, 07:38:14 pm »
But TFileAction's "FileName" is protected, and TFileOpen does not change its visibility, i.e. TFileOpen.FileName is still protected. You can access protected properties only from ancestors of the class which introduced them, but not from outside. An often-found trick is to inherit from the class with the protected property and to re-declare the property as public. Then a type-cast to the new class will give you access to the initially protected property:

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyFileOpen = class(TFileOpen)
  3.   public
  4.     property FileName;
  5.   end;
  6.  
  7. procedure TForm1.FileOpen1Accept(Sender: TObject);
  8. begin
  9.   ShowMessage(TMyfileOpen(FileOpen1).FileName);
  10. end;

Certainly this is some kind of hack because the programmer must have had something in mind when the property was declared only as protected.

simsee

  • Full Member
  • ***
  • Posts: 230
Re: Trouble with TFileOpen standard action
« Reply #6 on: October 02, 2016, 08:17:00 pm »
Now I understand. Your answer is very clear! Thanks again!

 

TinyPortal © 2005-2018