Recent

Author Topic: Add additional Components to a TOpenDialog  (Read 816 times)

dangersgromit

  • Newbie
  • Posts: 4
Add additional Components to a TOpenDialog
« on: January 16, 2025, 12:26:58 am »
Hello everyone!

I'm kind of stuck trying to add a combobox to a TFileDialog (in this case a TOpenDialog). I tried to adapt an old Delphi example I found on StackOverflow to Lazarus.
Everythings seems fine, but i get an "Error: identifier idents no member "QueryInferace" while compiling on lines 57 and 68

It looks fine to me when i follow the function back to TComponent. Even the IDE presents these help lines:
Quote
protected function TComponent. QueryInterface(constref IID: TGUID; out Obj): Hresult;
C:\lazarus40\fpc\3.2.2\source\rtl\objpas\classes\classesh.inc(1941,14)
unit Classes

Any ideas?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Messages, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Variants, win32Extra;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     btnOpenFile: TButton;
  16.     ListBox1: TListBox;
  17.     ExampleDialog: TOpenDialog;
  18.     procedure btnOpenFileClick(Sender: TObject);
  19.     procedure ExampleDialogCanClose(Sender: TObject; var CanClose: Boolean);
  20.     procedure ExampleDialogShow(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. const
  31.   CUSTOM_CONTROL_ID = 1;
  32.  
  33. implementation
  34.  
  35. uses
  36.   shlobj;
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.btnOpenFileClick(Sender: TObject);
  43. begin
  44.   ExampleDialog.Tag := 0;
  45.   if ExampleDialog.Execute then
  46.   begin
  47.     ShowMessage (Format ('Selected Custom Item %d'#13#10+'File: %s', [ExampleDialog.Tag, ExampleDialog.Filename]));
  48.   end;
  49. end;
  50.  
  51. procedure TForm1.ExampleDialogCanClose(Sender: TObject; var CanClose: Boolean);
  52. var
  53.   ICustomize : IFileDialogCustomize;
  54.   vSelectedIndex : DWORD;
  55. begin
  56.   vSelectedIndex:= 0;
  57.   if ExampleDialog.QueryInterface(IFileDialogCustomize, ICustomize) = S_OK then
  58.     ICustomize.GetSelectedControlItem(CUSTOM_CONTROL_ID, vSelectedIndex);
  59.   ExampleDialog.Tag := vSelectedIndex -1;
  60. end;
  61.  
  62. procedure TForm1.ExampleDialogShow(Sender: TObject);
  63. var
  64.   ICustomize : IFileDialogCustomize;
  65.   vItem : String;
  66.   i: Integer;
  67. begin
  68.   if ExampleDialog.QueryInterface(IFileDialogCustomize, ICustomize) = S_OK then
  69.   begin
  70.     ICustomize.StartVisualGroup(0, 'Custom Items: ');
  71.     try
  72.       ICustomize.AddComboBox(CUSTOM_CONTROL_ID);
  73.  
  74.       for i := 0 to ListBox1.Count -1 do
  75.       begin
  76.         vItem := ListBox1.Items[i];
  77.         ICustomize.AddControlItem(CUSTOM_CONTROL_ID, i+1, PWideChar(vItem));
  78.       end;
  79.       if ListBox1.ItemIndex > -1 then
  80.       ICustomize.SetSelectedControlItem(CUSTOM_CONTROL_ID, ListBox1.ItemIndex+1);
  81.     finally
  82.       ICustomize.EndVisualGroup;
  83.     end;
  84.   end;
  85. end;
  86.  
  87. end.    
  88.  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1460
    • Lebeau Software
Re: Add additional Components to a TOpenDialog
« Reply #1 on: January 16, 2025, 03:52:59 am »
I'm kind of stuck trying to add a combobox to a TFileDialog (in this case a TOpenDialog). I tried to adapt an old Delphi example I found on StackOverflow to Lazarus.
Everythings seems fine, but i get an "Error: identifier idents no member "QueryInferace" while compiling on lines 57 and 68

QueryInterface() is protected in TComponent, in both Delphi and FreePascal.  So, you can't call it directly from outside of the class.  However, TComponent inherits a public GetInterface() method from TObject, and TComponent.QueryInterface() uses TObject.GetInterface() internally.

However, you should use SysUtils.Supports() instead.  Even in Delphi, this is the preferred way to query objects for interfaces.

However, TOpenDialog itself does not implement IFileDialogCustomize, so I would expect any of these query methods to fail.  It is the IFileDialog object that is wrapped inside of the TOpenDialog which implements IFileDialogCustomize.  So, you would need to query that IFileDialog object instead.

But, I don't think TOpenDialog in FreePascal/Lazarus exposes access to the wrapped IFileDialogTOpenDialog in Delphi certainly doesn't. Delphi has a whole separate TFileOpenDialog component which does expose access to IFileDialog, but FreePascal/Lazarus does not have that component.

I don't know which StackOverflow example you based your code on, but I don't see how it could ever have worked in Delphi, let alone in FreePascal/Lazarus.
« Last Edit: January 16, 2025, 03:55:45 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

dangersgromit

  • Newbie
  • Posts: 4
[Solved] Add additional Components to a TOpenDialog
« Reply #2 on: January 16, 2025, 04:26:41 am »
Thanks Remy.

The original example used TFileOpenDialog indeed.
So there is no "easy" way to do this in lazarus.

For this project I'll have to do another way. Instead of adding a Combobox do the dialog i'll have to do a second dialog afterwards asking for the additional information.

Greetings Klaus.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1460
    • Lebeau Software
Re: [Solved] Add additional Components to a TOpenDialog
« Reply #3 on: January 16, 2025, 05:08:22 am »
So there is no "easy" way to do this in lazarus.

For this project I'll have to do another way. Instead of adding a Combobox do the dialog i'll have to do a second dialog afterwards asking for the additional information.

Or, you could simply use IFileOpenDialog directly.

https://learn.microsoft.com/en-us/windows/win32/shell/common-file-dialog
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Add additional Components to a TOpenDialog
« Reply #4 on: January 16, 2025, 06:39:53 am »
And
https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ifiledialogcustomize and family. Note all Windows only.
I am not sure these API's have already a Pascal equivalent, not in Windows nor in jwaWindows.
But I am sure they don't want the Trumps back...

cdbc

  • Hero Member
  • *****
  • Posts: 1808
    • http://www.cdbc.dk
Re: Add additional Components to a TOpenDialog
« Reply #5 on: January 16, 2025, 07:03:25 am »
Hi
Hmmmm, MeThinks that will take some doing to get right... Not just bolted together in a hurry...
That method-list is worthy of a /dang/ Michelin-star  ;D
"Dialog on the fly" ...that's an idea  :P
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018