Recent

Author Topic: Change btn icon  (Read 3496 times)

Pe3s

  • Jr. Member
  • **
  • Posts: 54
Change btn icon
« on: October 27, 2021, 09:14:31 pm »
Hello, how can I change imageindex.  play -pause - and back play
« Last Edit: October 28, 2021, 08:11:05 am by Pe3s »

speter

  • Full Member
  • ***
  • Posts: 227
Re: Change imageindex
« Reply #1 on: October 28, 2021, 12:03:30 am »
G'Day Pe3s,

I think you need to include some more information, if you really want any help.

Is "imageindex" a local variable you have created? Or is it some object's property or method?

Also, I might be helpful to specify what version of Lazarus / FPC you are using and what OS (& version) you are using.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

Pe3s

  • Jr. Member
  • **
  • Posts: 54
Re: Change imageindex
« Reply #2 on: October 28, 2021, 07:38:00 am »
My point is that when you click on the button, the icon changes, when you click again, the icon returns

Pe3s

  • Jr. Member
  • **
  • Posts: 54
Re: Change imageindex
« Reply #3 on: October 28, 2021, 07:52:24 am »
Code: Pascal  [Select][+][-]
  1. var
  2. im: Boolean;
  3. begin
  4. if im then
  5. btn.ImageIngex :=1
  6. else
  7. btn.ImageIndex := 2;
  8. end
  9.  

howardpc

  • Hero Member
  • *****
  • Posts: 3834
Re: Change btn icon
« Reply #4 on: October 28, 2021, 10:14:49 am »
Drop a bitbtn and an imagelist on the main form of a new project.
Populate the imagelist as desired, and connect it to the button's Images property in the Object Inspector.
Complete the button's OnClick and form's OnCreate as follows:
Code: Pascal  [Select][+][-]
  1. unit uMain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Forms, Controls, Buttons;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     BitBtn: TBitBtn;
  13.     ImageList: TImageList;
  14.     procedure BitBtnClick(Sender: TObject);
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.     alternativeImage: Boolean;
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. procedure TForm1.BitBtnClick(Sender: TObject);
  26. var
  27.   btn: TBitBtn absolute Sender;
  28. begin
  29.   if (ImageList.Count < 2) or not Sender.InheritsFrom(TBitBtn) then
  30.     Exit;
  31.   case alternativeImage of
  32.     True:  btn.ImageIndex := 0;
  33.     False: btn.ImageIndex := 1;
  34.   end;
  35.   alternativeImage := not alternativeImage;
  36. end;
  37.  
  38. procedure TForm1.FormCreate(Sender: TObject);
  39. begin
  40.   BitBtnClick(BitBtn);
  41. end;
  42.  
  43. {$R *.lfm}
  44.  
  45. end.
« Last Edit: October 28, 2021, 11:30:34 am by howardpc »

Pe3s

  • Jr. Member
  • **
  • Posts: 54
Re: Change btn icon
« Reply #5 on: October 28, 2021, 06:09:51 pm »
@ howardpc Thank You  :)

Pe3s

  • Jr. Member
  • **
  • Posts: 54
Re: Change btn icon
« Reply #6 on: October 28, 2021, 07:13:31 pm »
I am also interested in how you can still change the icons after clicking on the button using the TSpeedButton and ImageList components, something like the speaker icon and the crossed out speaker

howardpc

  • Hero Member
  • *****
  • Posts: 3834
Re: Change btn icon
« Reply #7 on: October 28, 2021, 08:43:23 pm »
My point is that when you click on the button, the icon changes, when you click again, the icon returns
I am also interested in how you can still change the icons after clicking on the button using the TSpeedButton and ImageList components, something like the speaker icon and the crossed out speaker


I don't see what difference there is between these two scenarios.

Pe3s

  • Jr. Member
  • **
  • Posts: 54
Re: Change btn icon
« Reply #8 on: October 28, 2021, 09:34:51 pm »
If I want to run the procedure BitBtnClick (BitBtn); e.g. from another button it will not cause the program to malfunction?

howardpc

  • Hero Member
  • *****
  • Posts: 3834
Re: Change btn icon
« Reply #9 on: October 28, 2021, 10:37:35 pm »
An advantage of a RAD environment like Lazarus is that you can try things out usually very quickly to see what happens.
Drop a standard button on the project I suggested earlier. Name it SecondaryButton. Double-click the button to generate an OnClick handler for it, and complete the skeleton code Lazarus writes for you:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SecondaryButtonClick(Sender: TObject);
  2. begin
  3.   BitBtnClick(BitBtn);
  4. end;
Try it, and see how it behaves.

Pe3s

  • Jr. Member
  • **
  • Posts: 54
Re: Change btn icon
« Reply #10 on: October 29, 2021, 06:25:29 pm »
Thank you for solving the problem.
Regards

 

TinyPortal © 2005-2018