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?

Recommended Answers

All 7 Replies

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

TButton(Self).Color := clYellow;

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....

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

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

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

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

TButton(self).Caption := 'Hello';

Then it changes my Form's caption to hello

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.

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;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.