Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Solution to How to determine what button was pressed



Permlink Replies: 0
tim crouse

Posts: 83
Registered: 2/11/02
Solution to How to determine what button was pressed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 5, 2014 4:42 AM
Here is the solution code to this question - Thank You to all those that helped me along the way:

-Tim C.

Form Setup
1 radio button group with 2 radio buttons, named RB1, RB2
3 TButtons, Named Btn1, Btn2, Btn3
1 BitMap Close Button

Code..............................

NOTE - Select all buttons at once then set the on-click event
-copy the buttonpressed procedure to the clippboard (paste it to a notepad, delete it from the code, copy it to the clippboard)
-Select all the buttons on the GUI
-Set the on-click event
-Paste the code from the clippboard into the onclick procedure
-if you want to rename the procedure do it from the OI (object inspector) so the link does not get broke to all the buttons

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons;

type
TForm1 = class(TForm) //Form Type Declaration
GroupBox1: TGroupBox;
RB1: TRadioButton;
BitBtn1: TBitBtn;
Btn1: TButton;
Btn2: TButton;
RB2: TRadioButton;
Btn3: TButton;
procedure ButtonPressed(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

function IndexOf(aObj: TObject; const A: array of TObject): integer;

var // Global Variables
Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.ButtonPressed(Sender: TObject);
begin

//capture sender and assign to case

case IndexOf(Sender, [RB1, RB2, Btn1, Btn2]) of
0: ShowMessage('RB1 Pressed');
1 : ShowMessage('RB2 Pressed');
2 : ShowMessage('Btn1 Pressed');
3 : ShowMessage('Btn2 Pressed');
else ShowMessage('Not Sure What Button Was Pressed!');
// not one of the expected objects
end;

end;

function IndexOf(aObj: TObject; const A: array of TObject): integer;
var
I: Integer;
begin
Result := -1;
for I := Low(A) to High(A) do
if A[I] = aObj then begin
Result := I;
Break;
end; {if}
end;

end.

Edited by: tim crouse on Dec 5, 2014 5:03 AM

Edited by: tim crouse on Dec 5, 2014 5:05 AM

Edited by: tim crouse on Dec 5, 2014 5:12 AM

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02