[Lazarus] Checkbox value in TDrawGrid

Leonardo M. Ramé l.rame at griensu.com
Sat Oct 20 13:25:09 CEST 2012


On 2012-10-19 17:09:43 -0700, Jesus Reyes wrote:
> 
> 
> --- El vie 19-oct-12, Leonardo M. Ramé <l.rame at griensu.com> escribió:
> 
> > De: Leonardo M. Ramé <l.rame at griensu.com>
> > Asunto: [Lazarus] Checkbox value in TDrawGrid
> > A: "Lazarus mailing list" <lazarus at lists.lazarus.freepascal.org>
> > Fecha: viernes, 19 de octubre de 2012, 15:08
> > Hi, is there a method to set/get the
> > value of a checkbox in a TDrawGrid,
> > when the column's buttonStyle was declared as
> > cbsCheclBoxColumn?.
> > 
> 
> Since r39142 DrawGrid have the events OnGetCheckboxState and OnSetCheckboxState previously available only in StringGrid.
> 

Thanks, here are the steps to add CheckBoxes to a TDrawGrid:

1) Use Lazarus r39142 or newer.
2) Place a TDrawGrid on a form, and add goEditing to Options.
3) Add a column whith ButtonStyle = cbsCheckBoxColumn.
4) Add onDrawCell event, as usual.
5) Add onCheckBoxToggled, for example:

  procedure TForm1.DrawGridListCheckboxToggled(sender: TObject; aCol,
    aRow: Integer; aState: TCheckboxState);
  var
    lData: TMyList;

  begin
    if (ARow > 0) and (ACol = 0) then
    begin
      lData := FMyList[ARow - 1];
      lData.Checked := not lData.Checked;
    end;
  end;
  
  Here I have a list of items (FMyList) where each item has a "Checked"
  property which is setted to true or false each tiem the user clicks on
  a grid's checkbox. In the example the checkbox must be placed in the
  1st column.

6) Add an onGetCheckBoxState event:

  procedure TForm1.DrawGridListGetCheckboxState(Sender: TObject; ACol,
    ARow: Integer; var Value: TCheckboxState);
  var
    lData: TMyList;

  begin
    if ACol = 0 then
    begin
      lData := FMyList[ARow - 1];
      if lData.Checked then
        Value := cbChecked
      else
        Value := cbUnchecked;
    end;
  end;

  This event just check the status of an FMyList item and sets the param
  "Value" to cbChecked or cbUnchecked.

That's it.
-- 
Leonardo M. Ramé
http://leonardorame.blogspot.com




More information about the Lazarus mailing list