/examples/gridexamples/title_images/main.pas

http://github.com/graemeg/lazarus · Pascal · 89 lines · 68 code · 17 blank · 4 comment · 7 complexity · fec4b1e481467e004c3522973e37d624 MD5 · raw file

  1. unit main;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. SysUtils, Forms, Controls, Grids, ExtCtrls;
  6. type
  7. { TForm1 }
  8. TForm1 = class(TForm)
  9. ImageList1: TImageList;
  10. RadioGroup1: TRadioGroup;
  11. StringGrid1: TStringGrid;
  12. procedure RadioGroup1Click(Sender: TObject);
  13. procedure StringGrid1HeaderClick(
  14. Sender: TObject; IsColumn: Boolean;Index: Integer);
  15. private
  16. procedure Refresh;
  17. public
  18. end;
  19. var
  20. Form1: TForm1;
  21. implementation
  22. {$R main.lfm}
  23. uses
  24. Buttons;
  25. { TForm1 }
  26. procedure TForm1.RadioGroup1Click(Sender: TObject);
  27. var
  28. i: Integer;
  29. begin
  30. for i := 0 to StringGrid1.Columns.Count - 1 do begin
  31. if RadioGroup1.ItemIndex>1 then
  32. StringGrid1.RowHeights[0] := 2*StringGrid1.DefaultRowHeight
  33. else
  34. StringGrid1.RowHeights[0] := StringGrid1.DefaultRowHeight;
  35. StringGrid1.Columns[i].Title.ImageLayout :=
  36. TButtonLayout(RadioGroup1.ItemIndex);
  37. end;
  38. end;
  39. procedure TForm1.Refresh;
  40. var
  41. i, j: Integer;
  42. t: String;
  43. begin
  44. with StringGrid1 do
  45. for i := 1 to RowCount - 2 do
  46. for j := i + 1 to RowCount - 1 do begin
  47. if
  48. (Columns[0].Title.ImageIndex = 1) and (Cells[1, i] > Cells[1, j]) or
  49. (Columns[0].Title.ImageIndex = 2) and (Cells[1, i] < Cells[1, j]) or
  50. (Columns[1].Title.ImageIndex = 1) and (StrToInt(Cells[2, i]) > StrToInt(Cells[2, j])) or
  51. (Columns[1].Title.ImageIndex = 2) and (StrToInt(Cells[2, i]) < StrToInt(Cells[2, j]))
  52. then begin
  53. t := Cells[1, i]; Cells[1, i] := Cells[1, j]; Cells[1, j] := t;
  54. t := Cells[2, i]; Cells[2, i] := Cells[2, j]; Cells[2, j] := t;
  55. end;
  56. end;
  57. end;
  58. procedure TForm1.StringGrid1HeaderClick(
  59. Sender: TObject; IsColumn: Boolean; Index: Integer);
  60. begin
  61. if not IsColumn then exit;
  62. with StringGrid1.Columns[Index - 1].Title do begin
  63. if ImageIndex = 2 then
  64. ImageIndex := 0
  65. else
  66. ImageIndex := ImageIndex + 1;
  67. if ImageIndex > 0 then
  68. StringGrid1.Columns[2 - Index].Title.ImageIndex := 0;
  69. end;
  70. Refresh;
  71. end;
  72. end.