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!

How can I access a component caption dynamically? 1

Status
Not open for further replies.

planix

Technical User
Dec 3, 2002
22
Hi,

I have a form with a multiple number of labels on it. Each label is named "l1" "l2" ... "ln". I need to select a label at random to display some text.

So I want to randomly generate a number between 1 and n - easy.

Then I want to use that number to point to the label with that number in it's name so as to change the caption property of that label... this bit I am not at all sure about.

My dummy code idea would look something like this...

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  alabelobject: TLabel;
  i: integer;

begin

  alabelobject := TLabel.Create();

//randomly select one of the letters from the alphabet and //then display in the caption
  Randomize;

  i:=Random(48)

  alabelobject.name(i).Caption := alphabet.Strings[Random(25)];

end;

But of course this is doomed to fail. So, does anyone know how I can do this?

Cheers

Alistair
Townsville, Qld
 
Hi.
This code example has no Random code.
The fun thing is you can add a object into a StringList, and search for object captions by the use of IndexOf...


Good luck...
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  LabelList: TStringList;
  i: Integer;
begin
  LabelList := TStringList.Create;

  // Add a List to hold every TLabel
  for i := 0 to ControlCount -1 do
    if Components[i].ClassType = TLabel then
      LabelList.AddObject(TLabel(Components[i]).Caption, Components[i]);

  // Search for Label3
  i := LabelList.IndexOf('Label3');

  if i <> -1 then
  begin
    // Label3 was found, now change caption
    TLabel(LabelList.Objects[i]).Caption := 'Threre it was!';

    // Remember to change the List too, if you want to continue to use indexof.....
    LabelList.[i].Caption := 'Threre it was!';
  end;

  // clean up
  LabelList.Clear;
  LabelList.Free;
end;

KungTure-RX.jpg

//Nordlund
 
Take a look at the 'findcomponenet' method of whatever it is that has the buttons on it (the main form perhaps)
This code changes an image on a panel which is itself on another panel.
The panels are called 'Motor' plus a number and 'Mpars' + a number.

Code:
var Panel,Button: TComponent;
begin
   Panel := findcomponent('motor' + inttostr(Address));
   {test for nil return here to prevent crashes}
   if (panel <> nil)  then
      begin
         with Panel do
             begin
                Button := findcomponent('Mpars' + inttostr(Address));
                Tbitbtn(Button).glyph.Assign(nil);
                MotorImages.GetBitmap(0,  TbitBtn(Button).Glyph);
              end;

Steve

Life is like a Grapefruit, sort of orangey-yellow and dimpled on the outside, wet and squidgy in the middle, it's got pips inside too. Oh and some people have half a one for breakfast. Ford Prefect.

Want to do more with TGML Download Star
 
Hi,

Thanks for the ideas. They were both really helpful and gave me the push in the right direction.

The simplest solution, though I haven't implemented it fully- just testing the idea, was to use the FindComponents method.

Here is what I have so far;

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  aLabel:TComponent;
  i:integer;

begin
//randomly select one of the letters from the alphabet and then display in
//the caption
  Randomize;
  i:= Random(2);
  aLabel:= findcomponent('l' + IntToStr(i+1));
  TLabel(aLabel).Caption := alphabet.Strings[Random(25)];

end;

It seems very simple and straightforward so I should be able to scale it up from the testbed of two labels and I can see how I can hive it off into a separate procedure.

Thanks to both of you.

Alistair
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top