Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using string as a component name 1

Status
Not open for further replies.

Jiminald

Programmer
Oct 11, 2006
39
GB
Hi,

Im using a database for all of the button values, but i also use the database to draw the amount of buttons needed on the form. So i use the prefix "btn" and then the buttonID.
i.e. btn1 btn2 btn3 etc...

I need to change the caption of the button after the button has been modified through a form of mine.

I hope this makes sense to anyone
TIA
Jiminald :)
 
If you are creating buttons 'on the fly' they can be named as they are created.

This code creates panels and positions them on screen, the principle is the same.
They will be names Motor1,motor2, motor3, etc

Code:
 Motor:= Tpanel.Create(MotorControl);
  with motor do
    begin
       name := 'Motor'+inttostr(count);
       Enabled := False;
       Parent := MotorControl;
       Width := 186;
       Height := 113;
       Top := 27 + Mx;
       Left := 10 + My;
       Caption := '';
    end;
  inc(My,198);
  if My > 600 then
     begin
       My := 0;
       inc(Mx,121);
     end;

If you want to rename an existing object destroy and recreate it with the new name.

Code:
 oldmotor := Findcomponent('Motor'+inttostr(count));
  oldmotor.free;

if you just want to change the caption use the .caption property.



Steve [The sane]: Delphi a feersum engin indeed.
 
What is motor and oldmotor assigned to?

I have mine going like
GenButton : Tbutton;

if yours going
motor, oldmotor : TComponent or TPanel?

Thanks :)
 
In this case 'motor' is a generic name used when creating the panels.

Motor is a TPanel object.

The line

name := 'Motor'+inttostr(count);

Assigns it the required name so that I can access each panel as separate object, as 'Count' gets incremented.
You can do the same with TButton.

'Oldmotor' here is a temporary Tcomponent object which is used by the Findcomponent function.

Here I am simply freeing the component but if I wanted to change a specific property of something that Findcomponent finds, I would have to cast it to the correct object type.

e.g (oldmotor as TPanel).caption := 'new caption'

The Delphi help explains how findcomponent works


Steve [The sane]: Delphi a feersum engin indeed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top