tags:

views:

477

answers:

3

I have a small button I want to add to the top left corner of a Delphi TDBGrid component (in the header/title cells). I am able to place the button easily, but now the click event is not handled. I'm guessing the event is being caught by the grid. Any way I can force this specific event to go to the button instead? Note I still need the grid to handle click events for its title buttons like it currently does.

procedure TForm38.FormCreate(Sender: TObject);
begin
   button1.Parent := grid;
   button1.Top := 0;
   button1.Left := 0;
   button1.Width := 12;
   button1.Height := 18;
   button1.OnClick := Button1Click;
end;

Update:I've found I was able to use the button's MouseDown event which seems to work well, but I couldn't use the click event.

procedure TForm38.Button1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
   if ( Button = mbLeft ) then
      TButton(Sender).Click;
end;
+1  A: 

Wow, this is a toughie. TDBGrid doesn't have a generic "OnClick" event handler, and the position of your button makes it difficult to fake it. The only thing I can think of is to create your own component. This isn't as difficult as it might sound at first. Descend from TDBGrid and override the MouseUp method. Take a look at the way TCustomDBGrid.MouseUp is implemented. I haven't tested this, but you could probably try something like this: After the if statement that tests for

(cell.x >= FIndicatorOffset)

and fires off click events, have it fire TitleClick(nil) or something if cell.x < FIndicatorOffset. Then attach an event handler to your grid's OnTitleClick event that calls Button1.Click if Column = nil. (Yeah, sort of a hack, but I can't think of an easier way to do it. If I were you I'd report this one to QC as a bug.)

Mason Wheeler
+1  A: 

I am not sure why it is eating the click message. You actually simulate the click event through the MouseUp event handler.

procedure TForm38.Button1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  ctrl: TButton;
begin
  ctrl := Sender as TButton;
  if (x > 0) and (x < ctrl.Width) and (y > 0) and (y < ctrl.Height)  then
    ctrl.Click;
end;

If you test a normal button you will see that the click event doesn't happen until you release the mouse. Simulating a click on button down creates odd behavior.

Jim McKeeth
Thanks Jim. I changed it to use a MouseUp event. MouseDown worked, but it wouldn't actually show the button being pressed, which was a little odd of you were paying close attention to it.
Jeremy Mullin
+1  A: 

The following is a clean solution, tried successfully both with Delphi 1 and Delphi 2007:

procedure TForm38.FormCreate(Sender: TObject);
begin
   button1.Parent := grid;
   button1.Top := 0;
   button1.Left := 0;
   button1.Width := 12;
   button1.Height := 18;
   button1.OnClick := Button1Click;

   // only add this line
   button1.ControlStyle := button1.ControlStyle + [csClickEvents];

end;
mghie