title: Why don't child controls of a TStringGrid work properly?
body: <p>I am placing checkboxes (<code>TCheckBox</code>) in a string grid (<code>TStringGrid</code>) in the first column. The checkboxes show fine, positioned correctly, and respond to mouse by glowing when hovering over them. When I click them, however, they do not toggle. They react to the click, and highlight, but finally, the actual <code>Checked</code> property does not change. What makes it more puzzling is I don't have any code changing these values once they're there, nor do I even have an <code>OnClick</code> event assigned to these checkboxes. Also, I'm defaulting these checkboxes to be <em>unchecked</em>, but when displayed, they are <em>checked</em>.</p>
  
  <p>The checkboxes are created along with each record which is added to the list, and is referenced inside a record pointer which is assigned to the object in the cell where the checkbox is to be placed.</p>
  
  <p>String grid hack for cell highlighting:</p>
  
  <pre><code>type
    THackStringGrid = class(TStringGrid); //used later...
  </code></pre>
  
  <p>Record containing checkbox:</p>
  
  <pre><code>  PImageLink = ^TImageLink;
    TImageLink = record
      ...other stuff...
      Checkbox: TCheckbox;
      ShowCheckbox: Bool;
    end;
  </code></pre>
  
  <p>Creation/Destruction of checkbox:</p>
  
  <pre><code>function NewImageLink(const AFilename: String): PImageLink;
  begin
    Result:= New(PImageLink);
    ...other stuff...
    Result.Checkbox:= TCheckbox.Create(nil);
    Result.Checkbox.Caption:= '';
  end;
  
  procedure DestroyImageLink(AImageLink: PImageLink);
  begin
    AImageLink.Checkbox.Free;
    Dispose(AImageLink);
  end;
  </code></pre>
  
  <p>Adding rows to grid:</p>
  
  <pre><code>//...after clearing grid...
  //L = TStringList of original filenames
  if L.Count &gt; 0 then
    lstFiles.RowCount:= L.Count + 1
  else
    lstFiles.RowCount:= 2; //in case there are no records
  for X := 0 to L.Count - 1 do begin
    S:= L[X];
    Link:= NewImageLink(S); //also creates checkbox
    Link.Checkbox.Parent:= lstFiles;
    Link.Checkbox.Visible:= Link.ShowCheckbox;
    Link.Checkbox.Checked:= False;
    Link.Checkbox.BringToFront;
    lstFiles.Objects[0,X+1]:= Pointer(Link);
    lstFiles.Cells[1, X+1]:= S;
  end;
  </code></pre>
  
  <p>Grid's OnDrawCell Event Handler:</p>
  
  <pre><code>procedure TfrmMain.lstFilesDrawCell(Sender: TObject; ACol, ARow: Integer;
    Rect: TRect; State: TGridDrawState);
  var
    Link: PImageLink;
    CR: TRect;
  begin
    if (ARow &gt; 0) and (ACol = 0) then begin
      Link:= PImageLink(lstFiles.Objects[0,ARow]); //Get record pointer
      CR:= lstFiles.CellRect(0, ARow); //Get cell rect
      Link.Checkbox.Width:= Link.Checkbox.Height;
      Link.Checkbox.Left:= CR.Left + (CR.Width div 2) - (Link.Checkbox.Width div 2);
      Link.Checkbox.Top:= CR.Top;
      if not Link.Checkbox.Visible then begin
        lstFiles.Canvas.Brush.Color:= lstFiles.Color;
        lstFiles.Canvas.Brush.Style:= bsSolid;
        lstFiles.Canvas.Pen.Style:= psClear;
        lstFiles.Canvas.FillRect(CR);
        if lstFiles.Row = ARow then
          THackStringGrid(lstFiles).DrawCellHighlight(CR, State, ACol, ARow);
      end;
    end;
  end;
  </code></pre>
  
  <p>Here's how it looks when clicking...</p>
  
  <p><img src="https://i.stack.imgur.com/zMgyk.png" alt="Reacts to Mouse Click but Doesn&#39;t Change"></p>
  
  <p>What could be causing this? It's definitely not changing the <code>Checked</code> property anywhere in my code. There's some strange behavior coming from the checkboxes themselves when placed in a grid.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I did a brief test, I placed a regular <code>TCheckBox</code> on the form. Check/unchecks fine. Then, in my form's <code>OnShow</code> event, I changed the Checkbox's <code>Parent</code> to this grid. This time, I get the same behavior, not toggling when clicked. Therefore, it seems that a <code>TCheckBox</code> doesn't react properly when it has another control as its parent. How to overcome this?</p>