?
Solved

Placing a CheckBox on a StringGrid or DrawGrid

Posted on 2003-03-05
8
Medium Priority
?
3,927 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

770 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