Recent

Author Topic: (SOLVED) Loop all buttons and add icons from TImageList  (Read 351 times)

AsleyCruz

  • Jr. Member
  • **
  • Posts: 78
    • Graphic and apps designer
(SOLVED) Loop all buttons and add icons from TImageList
« on: February 23, 2020, 06:41:38 pm »
Hello team
If I have several TButtons named Button1, Button2, Button3, Button4.
How can I look for all this buttons and apply them a different icon/glyph from a TImageList?

Thanks in advance.
« Last Edit: February 23, 2020, 10:43:32 pm by AsleyCruz »
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

wp

  • Hero Member
  • *****
  • Posts: 7043
Re: List all buttons controls
« Reply #1 on: February 23, 2020, 07:29:42 pm »
TButton has no image support, use TBitBtn or TSpeedButton instead. They have an Images property to which you can assign the image list. Then iterate through all Components of the form, check for type TBitBtn or TSpeedButton, cast to this type and finally assign the ImageIndex. See attached demo (it also contains a listbox to display all images of the imagelist.)
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

lucamar

  • Hero Member
  • *****
  • Posts: 2825
Re: List all buttons controls
« Reply #2 on: February 23, 2020, 09:47:57 pm »
As wp said, but for completenes sake, the way to iterate the controls in the form is something like:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DoSomeThing;
  2. var
  3.   i: Integer;
  4. begin
  5.   for i := 0 to ControlCount-1 do
  6.     if Controls[i].InheritsFrom(TSomeControlClass) do begin
  7.       { Do whatever with: Controls[i] as TSomeControlClass }
  8.     end;
  9. end;

Idem with Components and ComponentCount, if what you're looking for is not a control.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

AsleyCruz

  • Jr. Member
  • **
  • Posts: 78
    • Graphic and apps designer
Re: List all buttons controls
« Reply #3 on: February 23, 2020, 10:29:36 pm »
Many thanks for your support team.
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

wp

  • Hero Member
  • *****
  • Posts: 7043
Re: List all buttons controls
« Reply #4 on: February 23, 2020, 11:28:51 pm »
As wp said, but for completenes sake, the way to iterate the controls in the form is something like:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DoSomeThing;
  2. var
  3.   i: Integer;
  4. begin
  5.   for i := 0 to ControlCount-1 do
  6.     if Controls[i].InheritsFrom(TSomeControlClass) do begin
  7.       { Do whatever with: Controls[i] as TSomeControlClass }
  8.     end;
  9. end;

Idem with Components and ComponentCount, if what you're looking for is not a control.
I disagree - depending on what the OP wants to achieve. Components[ i] and ComponentCount are for all components on the form, irrespective of the container in which they sit, while Controls[ i] and ControlCount are for the controls which sit immediately on the form (i.e. controls on a panel on the the form are not counted). Also note the subtle difference between "component" and "control"- but this does not matter here, a button is a control as well as a component. And what is the problem with "is" instead of "InheritsFrom"?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

lucamar

  • Hero Member
  • *****
  • Posts: 2825
Re: List all buttons controls
« Reply #5 on: February 24, 2020, 10:04:10 am »
I disagree - depending on what the OP wants to achieve. Components[i] and ComponentCount are for all components on the form, irrespective of the container in which they sit, while Controls[i] and ControlCount are for the controls which sit immediately on the form (i.e. controls on a panel on the the form are not counted).

Yes, you're right. I was a bit too quick with my response and didn't check. Sorry :-[

Quote
And what is the problem with "is" instead of "InheritsFrom"?

None at all, just a question of personal preference; they are completely equivalent.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018