Solved

HOW TO display the name of the clicked object

Posted on 2006-06-28
9
224 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

22 Experts available now in Live!

Get 1:1 Help Now