Recent

Author Topic: [SOLVED] Get All Objects in a ToolBar  (Read 4433 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] Get All Objects in a ToolBar
« on: August 09, 2016, 12:31:23 pm »
Hi!
I am creating several ToolBars in Form1.
For a ToolBar with specific Tag I need to enable a button with specific name that has parent this ToolBar.
I can find the ToolBar by parsing the Form1 elements as below.

Code: Pascal  [Select][+][-]
  1. PROCEDURE TForm1.OnChange_EditVal(Sender : TObject);
  2. VAR
  3.   theTag : INTEGER;
  4.   i, j : INTEGER;
  5.   theparent : TWinControl;
  6.   element_name : STRING;
  7.  
  8. BEGIN
  9.   WITH (Sender as TCDEdit) DO
  10.     theTag := (Sender as TCDEdit).Tag;       //Get the Tag aka Row
  11.  
  12.   FOR i := 0 TO TWinControl(Form1).ControlCount - 1 DO
  13.     IF TWinControl(Form1).Controls[i] is TBCToolBar THEN
  14.     BEGIN
  15.       theparent := TWinControl(Form1).Controls[i];
  16.  
  17.       if theparent.Name := inttostr(theTag) then begin
  18.       FOR j := 0 TO TWinControl(theparent).Components[j] DO     //does not work:  main.pas(282,43) Error: Incompatible types: got "TComponent" expected "LongInt"
  19.       BEGIN
  20.         element_name := TWinControl(theparent).Controls[j].Name;
  21.  
  22.         IF element_name = btnPostUpdateName + IntToStr(theTag) THEN
  23.         BEGIN
  24.           TWinControl(Form1).Controls[i].Enabled := True;     //Enable the button
  25.           break;
  26.         END;
  27.       END;
  28.       end;
  29.     END;
  30. END;      
  31.  
  32.  
  33.  

How would I find the button once I have the ToolBar?

Thank you.
« Last Edit: August 09, 2016, 03:04:46 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

balazsszekely

  • Guest
Re: Get All Objects in a ToolBar
« Reply #1 on: August 09, 2016, 01:04:15 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.   I: Integer;
  3. begin
  4.   for I := 0 to ToolBar1.ButtonCount - 1 do
  5.     if ToolBar1.Buttons[I].Name = 'Test' then
  6.       ShowMessage('ok');
  7. end;

PS: This is for TToolBar, what is TBCToolbar?
« Last Edit: August 09, 2016, 01:08:33 pm by GetMem »

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Get All Objects in a ToolBar
« Reply #2 on: August 09, 2016, 01:40:14 pm »
TBCToolBar  is tool bar from BGRA Controls.

Please note that I do not know how to get a handler of a TBCToolBar type.
I just know the name of the TBCToolBar but I think to your code I would need to send as parameter a TBCToolBar reference which I do not know how to obtain.
From parsing Form1 I get a TComponent in TWinControl(Form1).Controls.
Please advise.

Lazarus 2.0.2 64b on Debian LXDE 10

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Get All Objects in a ToolBar
« Reply #3 on: August 09, 2016, 01:59:35 pm »
Thank you.

The reference send works with something like the below.

Code: Pascal  [Select][+][-]
  1. PROCEDURE TForm1.OnChange_EditVal(Sender : TObject);
  2. VAR
  3.   theTag : INTEGER;
  4.   i, j : INTEGER;
  5.  
  6. BEGIN
  7.  
  8.   FOR i := 0 TO TWinControl(Form1).ControlCount - 1 DO
  9.     IF TWinControl(Form1).Controls[i] is TBCToolBar THEN
  10.     BEGIN
  11.       with (TWinControl(Form1).Controls[i] as TBCToolBar ) do begin
  12.           for j:= 0 to (TWinControl(Form1).Controls[i] as TBCToolBar ).ButtonCount - 1 do begin
  13.             memo1.Append((TWinControl(Form1).Controls[i] as TBCToolBar ).Buttons[j].Name);
  14.             if (TWinControl(Form1).Controls[i] as TBCToolBar ).Buttons[j].Name = btnPostUpdateName + IntToStr(theTag) then begin
  15.                 (TWinControl(Form1).Controls[i] as TBCToolBar ).Buttons[j].Enabled:= true;
  16.             end;
  17.           end;
  18.       end;
  19.     END;
  20. END;  
  21.  
  22.  
Lazarus 2.0.2 64b on Debian LXDE 10

balazsszekely

  • Guest
Re: [SOLVED] Get All Objects in a ToolBar
« Reply #4 on: August 09, 2016, 02:07:27 pm »
You're welcome, I'm glad it's working! By the way, you figured out this one by yourself.  ;)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Get All Objects in a ToolBar
« Reply #5 on: August 09, 2016, 02:39:38 pm »
In the code below and attached the number of buttons in the toolbar is shown as zero although toolbar is parent to 4 buttons.
Please advise what I am missing as with other type of buttons I have different than zero values.
Thank you.

Code: Pascal  [Select][+][-]
  1. unit main;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls, CustomDrawnControls, StdCtrls, customdrawndrawers;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     CDButton1: TCDButton;
  18.     CDButton2: TCDButton;
  19.     CDButton3: TCDButton;
  20.     Memo1: TMemo;
  21.     ToolBar1: TToolBar;
  22.     procedure Button1Click(Sender: TObject);
  23.   private
  24.     { private declarations }
  25.   public
  26.     { public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.Button1Click(Sender: TObject);
  39. var
  40.   i, j:integer;
  41.  
  42. begin
  43.   memo1.Clear;
  44.   memo1.Append(CDButton1.Parent.Name);
  45.   memo1.Append(CDButton2.Parent.Name);
  46.   memo1.Append(CDButton3.Parent.Name);
  47.   memo1.Append(Button2.Parent.Name);
  48.  
  49.   j:=ToolBar1.ButtonCount;    //shows 0
  50.  
  51.   memo1.Append(inttostr(j));
  52.  
  53.   for i:= 0 to ToolBar1.ButtonCount - 1 do begin
  54.      memo1.Append(ToolBar1.Buttons[i].Name);
  55.   end;
  56. end;
  57.  
  58. end.
  59.  
Lazarus 2.0.2 64b on Debian LXDE 10

balazsszekely

  • Guest
Re: Get All Objects in a ToolBar
« Reply #6 on: August 09, 2016, 02:56:34 pm »
ButtonCount is zero because you place a TCDButton to a TToolBar. Combine TToolBar with TToolButton or TBCToolBar with TCDButton and you should be fine.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Get All Objects in a ToolBar
« Reply #7 on: August 09, 2016, 03:04:33 pm »
Now I see that ButtonCount and ComponentCount do not count but TToolButton.
ControlCount counts also other type of buttons.

Thank you!
Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018