Recent

Author Topic: images in stringgrid  (Read 5619 times)

madepabloh

  • Full Member
  • ***
  • Posts: 160
images in stringgrid
« on: August 19, 2014, 08:30:58 pm »
I am coming crazy after all the day surfing in internet and this forum tryint to locate how to show some icons on the fisrt row of an stringgrid.

All the icons are contained in a imagelist. And my idea is to add an emply row (row 0) on top of the stringgrid to show an icon depending of the content of the first row (row 1).

I show the stringgrid have an option titled: "titleimagelist" and i selected the imagelist i prepared containing all the icons. But now i am not able to see how to show the icons in row 0.

Any idea of what i should do??
Thanks!

Michl

  • Full Member
  • ***
  • Posts: 226
Re: images in stringgrid
« Reply #1 on: August 19, 2014, 09:01:08 pm »
Something like this would do the job:

Code: [Select]
procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol,
  aRow: Integer; aRect: TRect; aState: TGridDrawState);
begin
  if aCol=0 then
    ImageList1.Draw(StringGrid1.Canvas, aRect.Left, aRect.Top, ARow mod ImageList1.Count);
end;

You can use TitleImageList to show a image in the title of a column.
Code: [Select]
type
  TLiveSelection = (lsMoney, lsChilds, lsTime);
  TLive = Array[0..1] of TLiveSelection;

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: images in stringgrid
« Reply #2 on: August 19, 2014, 09:02:31 pm »
Or you can add columns, assign the imagelist to the TitleImageList of the grid and the images to the Imageindex of the Title of each column. And you must have FixedRows on (FixedRows=1).

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: images in stringgrid
« Reply #3 on: August 19, 2014, 09:05:20 pm »
There's an example at
../lazarus/examples/gridexamples/title_images/title_image_demo.lpi

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: images in stringgrid
« Reply #4 on: August 19, 2014, 09:33:09 pm »
Thanks so much guys! I really appreciate!
I will check your ideas. Thanks!!

madepabloh

  • Full Member
  • ***
  • Posts: 160
Re: images in stringgrid
« Reply #5 on: August 20, 2014, 12:22:19 am »
I checked the example and it nice, althought i need to click at the first time in the title row to see the icons for the first time. Anyway, it works.
checking this example as proposed by @howardpc , and taking into account what propose @wp, i created this code assigned to a speed button:

Code: [Select]
(...)
  StringGrid1.FixedRows := 1;
  StringGrid1.FixedCols := 0;

  for i := 0 to  StringGrid1.Columns.Count - 1 do
    begin
      StringGrid1.Columns[i].Title.ImageIndex := 0;
    end;

   StringGrid1.AutoSizeColumns;
(...)

It compiles, but the title row does not shows any icon. Neither before to click on each one of them...

Yes, the TitleImagelist option of the stringgrid points to Imagelist, and fixedrow = 1;
Do you know if i need to change any other option or change this code in some way?

About the StringGrid1DrawCell solution proposed by @Michl, It works! I need to study how to modify to fit my requirements, but at least, i see the icon on the title cells!
« Last Edit: August 20, 2014, 12:39:02 am by madepabloh »

 

TinyPortal © 2005-2018