Recent

Author Topic: Checkbox in TStringGrid  (Read 45017 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1461
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Checkbox in TStringGrid
« Reply #15 on: June 10, 2011, 09:36:27 am »
Under Description of editor styles read the section cbsCheckboxColumn. This seems quite simple, you don't have to provide a custom cell editor.
If this built-in editor does not satisfy your needs, use a check box for a custom cell editor. To see how custom cell editor works, there is a nice simple example in your_lazarus_directory/examples/gridexamples/gridcelleditor. This example uses a combo box, you will use check box instead.
There is a nice example how it is done in delphi. Read http://delphi.about.com/od/usedbvcl/l/aa081903a.htm and then http://delphi.about.com/od/usedbvcl/l/aa082003a.htm

CM630

  • Hero Member
  • *****
  • Posts: 858
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Checkbox in TStringGrid
« Reply #16 on: June 02, 2014, 11:56:05 am »
StringGrid1.Columns.Items[0].ButtonStyle:=cbsCheckboxColumn;
results in exception class 'EListError' ... List index (0) out of bounds.
I have 5 or 6 columns and at least 4 rows.
I see no reason why this is happening?
Лазар 1,8,2;W7 64bit or XP 32bit;FPC3,0,4;rev 57369

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: Checkbox in TStringGrid
« Reply #17 on: June 02, 2014, 12:04:52 pm »
Quote
I have 5 or 6 columns and at least 4 rows.
There are two approaches how to use columns of a TStringGrid.
The first, simplier, is just to set ColCount.
The second, more sophisticated (and more powerful), is to add columns via property Columns. Only this way you can use ButtonStyle property.
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 3176
Re: Checkbox in TStringGrid
« Reply #18 on: June 02, 2014, 12:12:51 pm »
The error is not arising from missing columns, but from missing (column) items. Did you try to create the item(s) via the Object Inspector columns editor, or in code?

CM630

  • Hero Member
  • *****
  • Posts: 858
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Checkbox in TStringGrid
« Reply #19 on: June 02, 2014, 12:22:28 pm »
The error is not arising from missing columns, but from missing (column) items. Did you try to create the item(s) via the Object Inspector columns editor, or in code?
I dragged and dropped the StringGrid on the frame.
I just tried:
   
Code: [Select]
StringGrid1.Cells[3,3]:='3;3';
StringGrid1.Columns.Items[3].ButtonStyle:=cbsCheckboxColumn;
First line is fine, the second one resulted in the same error message.


I will try Blaazen's proposal and report.
Edit: In the IDe object editor, I clicked on columns and I created 5 colums. Now    StringGrid1.Columns.Items[3].ButtonStyle:=cbsCheckboxColumn; works.
Probably I should use 
Code: [Select]
StringGrid1.Cells[3,3]:='3;3';
   If 3 < StringGrid1.VsisibelColCount  then StringGrid1.Columns.Items[3].ButtonStyle:=cbsCheckboxColumn;


Checkboxes were grayed and did not react on clicking on them. I added goEditing:= true to the StringGrid options. Now they are manually checkable, but other cells are also editable. Is it possible to set this behaviour per column/ cell or I have to emulate it some other way (i.e. OnClick will resuts in checking/unchecking?)
« Last Edit: June 02, 2014, 01:13:20 pm by paskal »
Лазар 1,8,2;W7 64bit or XP 32bit;FPC3,0,4;rev 57369

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: Checkbox in TStringGrid
« Reply #20 on: June 02, 2014, 12:33:20 pm »
Quote
For some reason the checkboxes are grayed and do not reqact on clicking on them.
Set goEditing of StringGrid.Options to True in OI.
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

CM630

  • Hero Member
  • *****
  • Posts: 858
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Checkbox in TStringGrid
« Reply #21 on: June 02, 2014, 02:08:44 pm »
 
Quote
For some reason the checkboxes are grayed and do not reqact on clicking on them.
Set goEditing of StringGrid.Options to True in OI.

Now checkboxes are manually checkable, but other cells are also editable, which I do not want. Is it possible to set this behaviour per column/ cell or I have to emulate it some other way (i.e. OnClick will result in checking/unchecking?)

I added some info in the wiki.
Лазар 1,8,2;W7 64bit or XP 32bit;FPC3,0,4;rev 57369

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: Checkbox in TStringGrid
« Reply #22 on: June 02, 2014, 02:53:38 pm »
Quote
Is it possible to set this behaviour per column/ cell ...
Each column has boolean property ReadOnly.
If you need "per cell" then you really would need some hack, maybe OnBeforeSelection, don't know, I never tried that.
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/