Solved

HOW TO display the name of the clicked object

Posted on 2006-06-28
9
225 Views
Last Modified: 2010-04-05
Greetings.
I have like 100 buttons that are named from B1 to B100

I want to have one procedure that will identify the clicked button.

Imagine I want to display a message like, "you clicked on button 97"

One way is to have 100 diferent OnClick events and put a line there like:
Clicked_Button := '97' ;
showmessage ( "you clicked on button " + Clicked_Button);

But this is not good programing.

How can I know what is the object name I have clicked ?

Thanks in davnace for the help

Blu
0
Comment
Question by:blucaster
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 14

Assisted Solution

by:Hamidreza Vakilian
Hamidreza Vakilian earned 250 total points
ID: 17004304
Hi!

yes.....

you define some buttons on the form and create only one OnClick event for all of them...
when you click on a button you can get it's handle with a few lines of code....
then you can send a command to that handle to send it's caption.....


procedure TForm1.Button1Click(Sender: TObject);  {onclick event for all buttons}
var
h:thandle;
text:array [0..128] of char;

begin
h:=windowfrompoint(mouse.CursorPos); {get the handle of the button which is pressed}
SendMessage(h, WM_GETTEXT,
               Sizeof(Text), integer(@Text)) ;{get it's caption}
showmessage('You clicked on button '+text);
end;

Programmer-x
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 250 total points
ID: 17005211
set ALL the buttons to use the same "OnCLick event"
and just use ...

procedure TForm1.AnyButtonClick(Sender: TObject);
begin
    showmessage('you clikcked on button "' + (Sender as tbutton).Name + '"');
end;
0
 

Author Comment

by:blucaster
ID: 17005294
Thank you both for your quick support.

Both sugestions work but RealLoki solution is simpler.

However I learned something new from Programer-x that will surely be helpfull somewhen.

I hope none of you think I am beeing unfair but I am going to split the points.

Again, thanks.

Blu
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17005299
Boy, we can tell that Programmer-x isn't a real Delphi developer. ;-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:AshusCZ
ID: 17005305
Try this.
You need an empty form and its OnCreate event.

declaration part:

    procedure FormCreate(Sender: TObject);
    procedure ButtonClick(Sender: TObject);

the body:

procedure TForm1.FormCreate(Sender: TObject);
var i: byte;
    s: shortstring;
    b: TButton;
begin
for i := 1 to 100 do
   begin
   with b do
      begin
         b := TButton.Create(self);
         s := inttostr(i);
         Name := 'B'+s;
         SetBounds(10,i*11,80,10);
         OnClick := ButtonClick;
         Font.Size := 6;
         Caption := s;
         Form1.InsertControl(b);
      end;
   end;
end;

procedure TForm1.ButtonClick(Sender: TObject);
begin
ShowMessage('you clicked on button '+(Sender as TButton).Caption);
end;



It creates the buttons in run-time. And assigns the OnClick event to all buttons.
Btw. compared to the code of Programmer-x, this doesn't use Windows messages. ;-)
0
 

Expert Comment

by:AshusCZ
ID: 17005322
Guys, you're way too fast :) I haven't been able to write this any quicker..
0
 

Author Comment

by:blucaster
ID: 17005369
I am sorry Ashus...

I had assigned the points when I got your contribution.

I saved your code for future reference, so your effort wont have been in vain.

No points left though =(

Again, thanks...

Blu
0
 

Author Comment

by:blucaster
ID: 17009922
For the record the remark of EddieShipman was totally uncalled.

Everyone's help is commendable no mather how skilled one is.

Blu
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17011118
Oh, you didn't see the "grin". All experts here do a little "ribbing" on occasion.
Lighten up.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now