Recent

Author Topic: [SOLVED] change sub form icon at runtime  (Read 1656 times)

petevick

  • Sr. Member
  • ****
  • Posts: 392
[SOLVED] change sub form icon at runtime
« on: March 30, 2024, 10:04:59 am »
I'm trying to change the icon of a sub form at run time, I'm using the following code....

Code: Pascal  [Select][+][-]
  1.   ImageList1.GetBitmap(0, Image1.Picture.BitMap);
  2.   Form2.Icon.Assign(Image1.Picture.Graphic);
  3.  

....but I get an access violation error when I compile. There is definitely an image in ImageList1 and Image1 is declared  under TForm2 = class(TForm).
I'm beginning to think that it's not possible to change a sub form icon.
« Last Edit: March 30, 2024, 11:07:47 am by petevick »
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.4, FPC 3.2.2

Fibonacci

  • Sr. Member
  • ****
  • Posts: 453
  • Internal Error Hunter
Re: change sub form icon at runtime
« Reply #1 on: March 30, 2024, 10:10:37 am »
What is Unit2? Is it a form name? Or a unit? Then what is the Icon inside Unit2?

Try this

Code: Pascal  [Select][+][-]
  1. ImageList1.GetBitmap(0, Image1.Picture.Bitmap);
  2. Form2.Icon.Assign(Image1.Picture.Bitmap);
« Last Edit: March 30, 2024, 10:13:05 am by Fibonacci »

petevick

  • Sr. Member
  • ****
  • Posts: 392
Re: change sub form icon at runtime
« Reply #2 on: March 30, 2024, 10:14:01 am »
What is Unit2? Is it a form name? Or a unit? Then what is the Icon inside Unit2?

Try this

Code: Pascal  [Select][+][-]
  1. ImageList1.GetBitmap(0, Image1.Picture.Bitmap);
  2. Form2.Icon.Assign(Image1.Picture.Bitmap);
my bad, mistyped, Unit2 should be Form2  :-[
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.4, FPC 3.2.2

Fibonacci

  • Sr. Member
  • ****
  • Posts: 453
  • Internal Error Hunter
Re: change sub form icon at runtime
« Reply #3 on: March 30, 2024, 10:14:55 am »
So it works now or not?

petevick

  • Sr. Member
  • ****
  • Posts: 392
Re: change sub form icon at runtime
« Reply #4 on: March 30, 2024, 10:15:44 am »
So it works now or not?
sorry, no, it doesn't work
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.4, FPC 3.2.2

Fibonacci

  • Sr. Member
  • ****
  • Posts: 453
  • Internal Error Hunter
Re: change sub form icon at runtime
« Reply #5 on: March 30, 2024, 10:21:45 am »
Works for me :-\

petevick

  • Sr. Member
  • ****
  • Posts: 392
Re: change sub form icon at runtime
« Reply #6 on: March 30, 2024, 10:25:19 am »
Works for me :-\
Hmmm, there must be something in the rest of the code that's conflicting with it.  :-\
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.4, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 392
Re: change sub form icon at runtime
« Reply #7 on: March 30, 2024, 11:07:19 am »
Solved, it requires a physical TImage on the form rather that just declaring one  %)

Thanks Fibonacci for the gif that gave me the clue  ;)
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.4, FPC 3.2.2

Fibonacci

  • Sr. Member
  • ****
  • Posts: 453
  • Internal Error Hunter
Re: [SOLVED] change sub form icon at runtime
« Reply #8 on: March 30, 2024, 11:16:22 am »
Sure :) But it works without TImage on the form too

Code: Pascal  [Select][+][-]
  1. with TImage.Create(nil) do begin
  2.   ImageList1.GetBitmap(0, Picture.Bitmap);
  3.   Form2.Icon.Assign(Picture.Bitmap);
  4.   Free;
  5. end;

or

Code: Pascal  [Select][+][-]
  1. var
  2.   b: TBitmap;
  3. begin
  4.   Form2.Show;
  5.  
  6.   b := TBitmap.Create;
  7.   ImageList1.GetBitmap(0, b);
  8.   Form2.Icon.Assign(b);
  9.   b.Free;

TRon

  • Hero Member
  • *****
  • Posts: 3141
Re: change sub form icon at runtime
« Reply #9 on: March 30, 2024, 11:16:38 am »
Solved, it requires a physical TImage on the form rather that just declaring one  %)
Uhm, yes ? Classes requires to be instantiated ? see also manual.

So, either you use the designer to let the form do it automatically for you or manually as shown by Fibonacci.
« Last Edit: March 30, 2024, 11:18:59 am by TRon »
All software is open source (as long as you can read assembler)

petevick

  • Sr. Member
  • ****
  • Posts: 392
Re: [SOLVED] change sub form icon at runtime
« Reply #10 on: March 30, 2024, 12:19:48 pm »
Sure :) But it works without TImage on the form too

Code: Pascal  [Select][+][-]
  1. var
  2.   b: TBitmap;
  3. begin
  4.   Form2.Show;
  5.  
  6.   b := TBitmap.Create;
  7.   ImageList1.GetBitmap(0, b);
  8.   Form2.Icon.Assign(b);
  9.   b.Free;

I like that method, thanks Fibonacci for the help and guidance  ;)
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.4, FPC 3.2.2

 

TinyPortal © 2005-2018