954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Buttons help!!!!

I created buttons in my coding. 100 buttons.... Each one`s name is "Button" and the row code and then the column code... I wrote a procedure that is used by the OnClick event of those buttons. Now I want to get the name of the specific one I clicked on and the use that name to change only that button`s color. Help please?

Peppercat101
Newbie Poster
22 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

In the OnClick event there's Sender. That refers to the clicked button, so you can use

TButton(Self).Color := clYellow;
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

I cant seem to get that to work.... I created my button like this:

for l := 1 to 10 do
    for t := 1 to 10 do
    begin
    oButton := TButton.Create(Form1);
    with oButton do
      begin
      parent := Form1;
      Width := 80;
      Height := 30;
      left := l * width;
      top := t * Height;
      Name := 'Button' + IntToStr(l) + IntToStr(t);
      OnClick := MyButtonClick;
      end;


The MyButtonClick is my own Procedure that I wrote.... I tried that which you said but it wont work....

Peppercat101
Newbie Poster
22 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Have you placed the code from pritaeas in your MyButtonClick method?

jsosnowski
Junior Poster in Training
68 posts since Nov 2007
Reputation Points: 11
Solved Threads: 11
 
Have you placed the code from pritaeas in your MyButtonClick method?

Yes I did... I get an error... "Missing operator or semi-colon"...

Peppercat101
Newbie Poster
22 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Ok I was an idiot.... No error anymore but if i say:

[code TButton(self).Caption := 'Hello'; [/code]

Then it changes my Form`s caption to hello

Peppercat101
Newbie Poster
22 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

I do not understand your last statement. The Caption is the label that appears on the button. The name is the code name used to refer to the button. They can be the same or differ as you choose.

jsosnowski
Junior Poster in Training
68 posts since Nov 2007
Reputation Points: 11
Solved Threads: 11
 

Here is what you can do

procedure TForm1.MyButtonClick (Sender: TObject);
begin
TButton(Sender).Font.Color := clRed;
end;

Sender is the object that fired the event onCLick;

johnybe
Newbie Poster
1 post since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You