Solved

HOW TO display the name of the clicked object

Posted on 2006-06-28
9
231 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

807 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