Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Placing a CheckBox on a StringGrid or DrawGrid

Posted on 2003-03-05
8
Medium Priority
?
4,140 Views
Last Modified: 2012-06-27
I'm trying to place a checkbox on either of the grids. I set the checkbox's parent property to the grid and the box appears on the grid. However, When clicking it it does not get checked. Neither the Grid's OnClick nor the checkbox's event are triggered. When the mouse is pressed the box changes color to gray, indicating, I think, the it is receiving the event. I'm using BCB 5.0.
What am I overlooking?
0
Comment
Question by:Stav
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 8070683
wont work that way, You need grid with checkboxes not a standard grid, take a look at: www.torry.net or http://sourceforge.net/project/showfiles.php?group_id=71004
ziolko.
0
 

Author Comment

by:Stav
ID: 8070735
Why won't it work?
0
 

Author Comment

by:Stav
ID: 8070742
Sorry, why won't it work?
It works fine when I place the same checkbox on a memo, for instance...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:ziolko
ID: 8070791
hmm I think I misunderstood Your Q, You want place TCheckBox on  TDrawGrid right??
ziolko.
0
 
LVL 6

Accepted Solution

by:
DaFox earned 300 total points
ID: 8070919
Hi Stav.

First of all, you could take a look at this example that shows how to add comboboxes to stringgrid cells.

For adding checkboxes to a StringGrid I got two solutions:

1. using the genuine checkbox controls (a waste of window handles)

type
  TForm1 = class(TForm)
    StatusBar1: TStatusBar;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure StringGrid1TopLeftChanged(Sender: TObject);
    procedure gridcheckboxMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  chk: TCheckbox;
  i: integer;
begin
  with stringgrid1 do begin
    rowcount := 30;
    colcount := 10;
    for i:= fixedrows to rowcount-1 do begin
      cells[1,i] := ' ';
      chk:= TCheckbox.Create( stringgrid1 );
      with chk do begin
        name := format('CellChk%d_%d',[1,i]);
        caption := '';
        checked := false;
        visible := false;
        parent  := stringgrid1;
        width   := height;
        OnMouseDown := gridcheckboxMouseDown;
      end;
      Objects[1,i] := chk;
    end;
  end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  chk: TCheckbox;
  grid: TStringgrid;
begin
  grid := Sender As TStringgrid;
  if (aCol = 1) and (aRow >= grid.FixedRows)
  then begin
    chk:= TCheckbox(grid.Objects[ aCol, aRow ]);
    If Assigned(chk) Then Begin
      chk.SetBounds( (rect.left + rect.right - chk.width) div 2,
                     (rect.top + rect.bottom - chk.height) div 2,
                     chk.width, chk.height );
      if not chk.visible then
        chk.show;              
    End;
  end;

end;

type
  tgridcracker = Class( tstringgrid );

procedure TForm1.StringGrid1TopLeftChanged(Sender: TObject);
begin
  tgridcracker(Sender).InvalidateCol(1);
end;

procedure TForm1.gridcheckboxMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  With Sender As TCheckbox Do
    Checked := not Checked;
end;

end.

2. faking the checkboxes

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FCheck, FNoCheck: TBitmap;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  If not ( gdFixed In State ) and (aCol = 1) Then Begin
    With (Sender As TStringgrid).Canvas Do Begin
      brush.color := $E0E0E0;
      // checkboxes look better on a non-white background
      Fillrect( rect );
      If Odd(aRow) Then
        Draw( (rect.right + rect.left - FCheck.width) div 2,
              (rect.bottom + rect.top - FCheck.height) div 2,
              FCheck )
      Else
        Draw( (rect.right + rect.left - FNoCheck.width) div 2,
              (rect.bottom + rect.top - FNoCheck.height) div 2,
              FNoCheck )
    End;
  End;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  bmp: TBitmap;
begin
  FCheck:= TBitmap.Create;
  FNoCheck:= TBitmap.Create;
  bmp:= TBitmap.create;
  try
    bmp.handle := LoadBitmap( 0, PChar(OBM_CHECKBOXES ));
    // bmp now has a 4x3 bitmap of divers state images
    // used by checkboxes and radiobuttons
    With FNoCheck Do Begin
      // the first subimage is the unchecked box
      width := bmp.width div 4;
      height := bmp.height div 3;
      canvas.copyrect( canvas.cliprect, bmp.canvas, canvas.cliprect );
    End;
    With FCheck Do Begin
      // the second subimage is the checked box
      width := bmp.width div 4;
      height := bmp.height div 3;
      canvas.copyrect(
        canvas.cliprect,
        bmp.canvas,
        rect( width, 0, 2*width, height ));
    End;
  finally
    bmp.free
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FNoCheck.Free;
  FCheck.Free;
end

Best wishes,
Markus
0
 
LVL 6

Expert Comment

by:DaFox
ID: 8070923
Ooops, forgot the combobox example d/l! ;-)
-> http://www.scalabium.com/faq/strgrid.zip

Markus
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8071046
It's because TCustomGrid overrides WMCommand message handler and doesn't calls inherited, if not handled....
you can modify Grids.pas unit to:

procedure TCustomGrid.WMCommand(var Message: TWMCommand);
begin
  with Message do
  begin
    if (FInplaceEdit <> nil) and (Ctl = FInplaceEdit.Handle) then
    begin
      case NotifyCode of
        EN_CHANGE: UpdateText;
      end;
    end else
      inherited;
  end;
end;

save in directory of your project (or in Source\Patch path for example, and set browsing path to that dir) and rebuild your project.

wbr, mo.
0
 

Author Comment

by:Stav
ID: 8071122
Basically I did what DaFox suggested, using the mousedown event instead of the OnClick Event. That is why I gave him the points. However, mocarts' actually explained why that worked so I must thank him, too.
Thank you all for your quick response,

Stav
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question