2

How can simulate a mouse click on some cell in TDBGrid?

kobik
  • 21,001
  • 4
  • 61
  • 121
userPro
  • 59
  • 4
  • 12
  • 1
    @RBA: Doesn't seem to answer the OP's specific point, though? – MartynA Aug 25 '16 at 11:56
  • Why do you need to simulate clicking in a cell? If you know the Row and Column that you what to operate on, just write the code to do it. Then, if you want the same thing to happen when a cell is actually clicked, just call that code in the OnCellClick handler. Or are you unsure how to do that? – MartynA Aug 25 '16 at 12:08
  • @MartynA sometimes, it have a problem when i try to remove a record.. But when i select some cell in my current row, the remove is performed! .. – userPro Aug 25 '16 at 12:46
  • 1
    IMO, You are doing something wrong when you Delete a record, and now you want to do something worse to "solve" it. – kobik Aug 25 '16 at 12:53
  • If this is related to the problem you were having in this q http://stackoverflow.com/questions/39118837/delphi-dbgrid-delete-a-record, you'd do far better to get to the bottom of that problem and fix it. Btw, in view of what you've said, before pursuing what I've said in my answer to this q, try calling `DBGrid1.Perform(WM_LButtonUp, 0, 0) and see if your Delete goes ahead then. – MartynA Aug 25 '16 at 13:35
  • I've updated my answer to include some simpler code that doesn't involve the OnCellClick event. – MartynA Aug 25 '16 at 14:51

1 Answers1

4

Update:

This code should do what you seem to want:

type
  TMyDBGrid = class(TDBGrid);

function TForm1.GetCellRect(ACol, ARow : Integer) : TRect;
begin
  Result := TmyDBGrid(DBGrid1).CellRect(ACol, ARow);
end;

procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift:
    TShiftState; X, Y: Integer);
var
  Coords : TGridCoord;    
begin
  Coords := DBGrid1.MouseCoord(X, Y);
  Caption := Format('Col: %d, Row: %d', [Coords.X, Coords.Y]);
end;

procedure TForm1.SimulateClick(ACol, ARow : Integer);
type
  TCoords = packed record
    XPos : SmallInt;
    YPos : SmallInt;
  end;
var
  ARect : TRect;
  Coords : TCoords;
begin
  ARect := GetCellRect(ACol, ARow);
  Coords.XPos := ARect.Left;
  Coords.YPos := ARect.Top;
  DBGrid1.Perform(WM_LButtonUp, 0, Integer(Coords));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SimulateClick(StrToInt(edX.Text), StrToInt(edY.Text));
end;

The MouseCoord function of TDBGrid converts a pair of coordinates (X, Y) into a Column number (TGridCoord.X) and a Row number ((TGridCoord.Y).

The OnMouseUp event displays the results of calling DBGrid1.MouseCoord on the X & Y input arguments.

The SimulateClick simulates a click on a cell of the grid. It uses GetCellRect to get the coordinates (in the DBGrid) of the topleft of a specified cell and then calls Perform(WM_LButtonUp,...) on the DBGrid, passing the coordinates in the LParam argument.

Finally Button1Click calls SimulateClick using Col and Row values from a pair of TEdits. That causes the OnMouseUp event to fire and display the Col and Row number, so you can satisfy yourself that it has the same effect as mouse-clicking the corresponding cell.

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • when i use `DBGrid1.Perform(WM_LButtonDown, 0, Integer(Coords));` before the 'UP', I get what i need .. the cell with _Coords_ is selected and as 'focused' .. thx! – userPro Aug 25 '16 at 16:33
  • @userPro: Glad you sorted it out. Actually, I'd like to go back to your q about record deletion and see if we can solve that - I'll post you something in my answer there with some suggestions/queries about your q. – MartynA Aug 25 '16 at 17:07