Recent

Author Topic: How do I determine which button was clicked in dbgUsrEditButtonClick() ?  (Read 3856 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Platform Windows Server 2016, Lazarus 1.9  Fpc 3.1.1

I have a TDBGrid with two columns with type set to bsButtonColumn.  I have created a EditButtonClick() event for the TDBGrid.  The Sender to this event is the grid itself.  How do I determine which of the two buttons got clicked by the user?

procedure TfrmMngUsr.dbgUsrEditButtonClick(Sender: TObject);
begin
   if <what button?> then
        begin
        end;

   if <what other button?> then
        begin
        end;
end; 

balazsszekely

  • Guest
Re: How do I determine which button was clicked in dbgUsrEditButtonClick() ?
« Reply #1 on: January 03, 2018, 06:18:14 am »
procedure TfrmMngUsr.dbgUsrEditButtonClick(Sender: TObject);
begin
  ShowMessage(TDBGrid(Sender).DataSource.DataSet.FieldByName('YourField').AsString);
end;


RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: How do I determine which button was clicked in dbgUsrEditButtonClick() ?
« Reply #2 on: January 03, 2018, 06:57:45 am »
Not sure I'm following you GetMem.  I have two columns with buttons in them.  "Edit" and "Delete".  Using your code in the EditButtonClick() event:

If I use:
ShowMessage(TDBGrid(Sender).DataSource.DataSet.FieldByName('Edit').AsString);  // Shows me the word "Edit" (bc that's what the field contains) no matter which button I click.

If I use:
ShowMessage(TDBGrid(Sender).DataSource.DataSet.FieldByName('Del').AsString);  // Shows me the word "Delete" (bc that's what the field contains) no matter which button I click.

So, am I missing something?  I need to know which button the user clicked, ... the Edit buton or the Delete button... not just show the contents of a hard coded field name.

balazsszekely

  • Guest
Re: How do I determine which button was clicked in dbgUsrEditButtonClick() ?
« Reply #3 on: January 03, 2018, 07:41:13 am »
It wasn't clear from your first post(at least for me) what exactly are you trying to achieve. I believe I do understand now. Please add the following before your TfrmMngUsr class declaration:
Code: Pascal  [Select][+][-]
  1. type
  2.   TDBGrid = class(DBGrids.TDBGrid)
  3.   published
  4.     property OnButtonClick;
  5.   end;
then
Code: Pascal  [Select][+][-]
  1.  TfrmMngUsr= class(TForm)
  2.  //...
  3.  private
  4.    procedure OnButtonClick(Sender: TObject; aCol, aRow: Integer);
  5.  end;
  6.  
  7. //...
  8. procedure TfrmMngUsr.OnButtonClick(Sender: TObject; aCol, aRow: Integer);
  9. begin
  10.   ShowMessage('Column: ' + IntToStr(aCol) + sLineBreak +
  11.               'Row: ' + IntToStr(aRow) + sLineBreak +
  12.               'Field: ' + TDBGrid(Sender).DataSource.DataSet.FieldByName('NAME').AsString);
  13. end;
  14.  

Please note: you should change the "NAME" field to whatever field you're using. I attach a screenshot, to see what I mean.
Let me know if this method works for you.
 
« Last Edit: January 03, 2018, 08:01:29 am by GetMem »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: How do I determine which button was clicked in dbgUsrEditButtonClick() ?
« Reply #4 on: January 03, 2018, 09:18:11 am »
The sender is NOT the grid itself, but any of the buttons:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMngUsr.dbgUsrEditButtonClick(Sender: TObject);
  2. begin
  3.    if sender = button1  then
  4.    begin
  5.       Caption := 'Button1';
  6.    end
  7.    else if sender = button2  then
  8.    begin
  9.      Caption := 'Button2';
  10.    end;
  11. end;

This assumes you have connected this event to both buttons.
« Last Edit: January 03, 2018, 09:29:53 am by Thaddy »
Specialize a type, not a var.

balazsszekely

  • Guest
Re: How do I determine which button was clicked in dbgUsrEditButtonClick() ?
« Reply #5 on: January 03, 2018, 09:35:28 am »
Quote
@Thaddy
The sender is NOT the grid itself, but any of the buttons:
Try this:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMngUsr.dbgUsrEditButtonClick(Sender: TObject);
  2. begin
  3.   if not (Sender is TButton) then
  4.     ShowMessage('Thaddy I''m not  a TButton :D .');
  5. end;

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: How do I determine which button was clicked in dbgUsrEditButtonClick() ?
« Reply #6 on: January 03, 2018, 08:24:56 pm »
Hi Thaddy and GetMem.  GetMem is correct in saying that the Sender is the Grid and not the Edit button.  I think there is some confusing here.  I'm declaring a grid column, button style to be dbsButtonColumn.  This causes the entire cell to be filled with the text of the field (which is a calculated field) either the word "Edit" or the word "Delete".  This looks nice.  However, these "buttons" are not the same as the little "edit" button that appears as an ellipse in a column that is editable.  So, here's the results:

I suspect Thaddy's suggestion is for the little edit button that appears in an editable column.  GetMem is correct in that the Sender is the grid (in my case)  My entire grid is readonly, so those buttons don't appear.

GetMem's code:  First, thank you very much for the detailed explanation and screen shot.  Makes it very clear.

However the event is not triggering.  I have followed your code exactly.  My project compiles without error. But when clicking any cell or any button, the event doesn't fire.  I'm using Laz 1.9 and Fpc 3.1.1
I may just dump the button idea and use plain text with an underline, to make it look like a link.

balazsszekely

  • Guest
Re: How do I determine which button was clicked in dbgUsrEditButtonClick() ?
« Reply #7 on: January 03, 2018, 08:36:03 pm »
@RedOctober
Quote
However the event is not triggering.  I have followed your code exactly.
Of course not. I forget something crucial from my previous post.  Sorry for that.
Code: Pascal  [Select][+][-]
  1. procedure TfrmMngUsr.FormCreate(Sender: TObject);
  2. begin
  3.   dbgUsr.OnButtonClick := @OnButtonClick;
  4.   //...
  5. end;
I tested with a read only grid too and it works well.
« Last Edit: January 03, 2018, 09:31:40 pm by GetMem »

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: How do I determine which button was clicked in dbgUsrEditButtonClick() ?
« Reply #8 on: January 06, 2018, 06:51:12 pm »
Thank you GetMem.  I will use your code in a future project.  For now, I simply made a Calculated string field, with the word "Edit" in one column and "Delete" in the other column.  Then I made the text blue, with an underline, to make it look like a link.  I got it working and it looks nice and clear and simple.

Suggestion:  It would be nice if there was a TDBGrid column type of "URL" that would make the font in the field look like a URL automatically, following all the style settings in the OS.

balazsszekely

  • Guest
Re: How do I determine which button was clicked in dbgUsrEditButtonClick() ?
« Reply #9 on: January 06, 2018, 07:20:41 pm »
@RedOctober
Quote
thank you GetMem.  I will use your code in a future project.  For now, I simply made a Calculated string field, with the word "Edit" in one column and "Delete" in the other column.  Then I made the text blue, with an underline, to make it look like a link.  I got it working and it looks nice and clear and simple.
You're welcome! I'm glad it's working.

Quote
Suggestion:  It would be nice if there was a TDBGrid column type of "URL" that would make the font in the field look like a URL automatically, following all the style settings in the OS.
Perhaps you should file in a bug report with the request here: https://bugs.freepascal.org/my_view_page.php .

 

TinyPortal © 2005-2018