Solved

HOW TO display the name of the clicked object

Posted on 2006-06-28
9
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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