Solved

HOW TO display the name of the clicked object

Posted on 2006-06-28
9
228 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 26

Expert Comment

by:EddieShipman
ID: 17005299
Boy, we can tell that Programmer-x isn't a real Delphi developer. ;-)
0
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

11 Experts available now in Live!

Get 1:1 Help Now