user3272241
user3272241

Reputation: 67

Reassign an OnCellClick event

I have an SQLite database with many Tables and one is named "tblAccounts" I have a dlgCommon that has a TDBGrid on it with the dbgridAccounts.DataSource:=srcAccounts

I have several other Dialogs all of which at some time need to click a button and show the Accounts Grid to select an Account from. Rather than have many Forms all with their own TDBgrid.DataSource:=srcAccounts I am doing this...

procedure TdlgFolders.btnAcctSelClick(Sender: TObject);
begin
  dlgCommon.pnlAccounts.Parent:=Self;
  dlgCommon.pnlAccounts.Left:=dbedAccount.Left;
  dlgCommon.pnlAccounts.Top:=dbedAccount.Top+dbedAccount.Height+2;
  dlgCommon.pnlAccounts.Width:=190;
end;

When the user has the dlgFolders active and clicks "btnAcctSel" it all does as I need and shows the Grid. But, when the user clicks the Grid-Cell I am at a loss where/how to put the dbgridAccountsCellClick(Column: TColumn); Handler.

I tried putting it in the dlgCommon and it compiles, but is not used as that is no longer the Parent when the Grid is visible and Cell-clicked in one of the other Dialogs.

I would prefer to keep using the single-Grid approach as the user gets to set the column widths, Row-colors etc and I'd rather not make them do that in every Form where the Accounts Grid is needed.

How can I reassign the dlgCommon.AccountsCellClick so that the click is trapped and used in dlgFolders and other Dialogs that call it too?

Upvotes: 0

Views: 661

Answers (1)

Tom Brunberg
Tom Brunberg

Reputation: 21033

I'm not sure I folllow your structure and design, but I would place the grid that shows the accounts on a TFrame. This TFrame would hold all event handlers you need for the grid, in addition to the grid itself.

Then, whenever you need to show the grid, you instantiate the frame, assign its parent and the grid and event handlers are ready for use.


On a second and third reading, if dlgCommon is a form with a hierarchial structure like

  dlgCommon: TdlgCommon
    pnlAccounts: TPanel
      AccountsGrid: TDBGrid

it appears that you have tried to "rip-out" (by changing the parent) the pnlAccounts from that form and then the event handlers don't work, just as you have noticed.

The idea of changing a components parent like this is a really bad idea, because when you assign a new parent to the grid, it will ofcourse not anymore show up in dlgCommon. It can be visible in only one dialog at a time.

If you want the grid simultaneously visible on various forms for (at least) some period of time, I would still use a TFrame as I already suggested.

In this case you can add an OnCellClick event manually to the forms private section

procedure DBGridCellClick(Column: TColumn);

and implement it in the form

procedure TForm1.DBGridCellClick(Column: TColumn);
begin
  // whatever you want to do
end;

And you instantiate the frame as follows:

procedure TForm1.Button2Click(Sender: TObject);
begin
  frame:= TFrame3.Create(self);
  frame.Parent := self;
  frame.Left := 8;
  frame.Top := 75;
  frame.DBGrid1.OnCellClick := DBGridCellClick;
end;

If, on the other hand, the user needs to see the grid only briefly, to select an account (and be done with it), I would simply show the dlgCommon modally.

Upvotes: 0

Related Questions