Recent

Author Topic: Some question about TStringGrid.  (Read 904 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 666
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Some question about TStringGrid.
« on: November 15, 2023, 06:21:46 pm »
Hello.
  Given a TStringGrid on the form with some lines.
  the first column is cbsCheckboxColumn.
  So. 
   - Is there any way when checking this checkbox on current row, to uncheck all other lines ( I want to be able that only one checkbox on current row checked ).
   - Is there any way to move the rows without using fixed area or using code.

Thank you
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

jamie

  • Hero Member
  • *****
  • Posts: 7773
Re: Some question about TStringGrid.
« Reply #1 on: November 16, 2023, 01:41:21 am »
As for your first question:

 setting the check marks per row requires you to have an array of TCheckBoxState That maintains the values of each row.

 Using the "OnGetCheckBoxState" event, you can report the value of each of these indexs per aRow as it comes in.

 In other words, use the aROW as the index into the Array of TCheckBoxState you have.

 Using the "OnSetCheckBoxState" is where you define the Array of TCheckBoxState but here you can examine the current value of the incoming state and if it's ON (cbChecked), you set the value within the array of that aROW and set all the others in the Array to cbUnChecked.

 WHen the user clicks or toggles the state of a checkmark, the value will reflect when the "OnSetCheckBoxState" is called.

 If you need an example I could write one up.

As for your second request, I don't know. Not sure what you are really asking?

I decided to give you an example.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     StringGrid1: TStringGrid;
  16.     procedure StringGrid1GetCheckboxState(Sender: TObject; ACol, ARow: Integer;
  17.       var Value: TCheckboxState);
  18.     procedure StringGrid1SetCheckboxState(Sender: TObject; ACol, ARow: Integer;
  19.       const Value: TCheckboxState);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.   Checks:Array[0..10] of TCheckBoxState;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.StringGrid1GetCheckboxState(Sender: TObject; ACol,
  37.   ARow: Integer; var Value: TCheckboxState);
  38. begin
  39.   Value := Checks[ARow];
  40. end;
  41.  
  42. procedure TForm1.StringGrid1SetCheckboxState(Sender: TObject; ACol,
  43.   ARow: Integer; const Value: TCheckboxState);
  44. var
  45.   I:Integer;
  46. begin
  47.   checks[aRow]:= Value;
  48.   If Value = cbChecked then
  49.   Begin
  50.    For I := Low(Checks) to High(Checks) do
  51.     If I <> aRow THen Checks[I]:= cbUnchecked;
  52.    TStringGrid(Sender).Repaint;
  53.   end;
  54. end;
  55.  
  56. end.
  57.  

In that example, the first column is used (1).




« Last Edit: November 16, 2023, 01:53:52 am by jamie »
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018