Jump to content
Sign in to follow this  
Henry Olive

Getting Label Name from Label.Caption

Recommended Posts

I have 6 Labels on my form

I need to fill them according to a query result

but i dont know how many data returns ( may be NONE may be 1 may be 6 ( which is MAX)
I wrote below codes
 

var  MyLabel : TLabel;

 

  while not Query1.Eof do
  begin
    if Query1.Fields[0].AsString <> '' then
    begin
      MyLabel.Caption :=Query1.Fields[0].AsString + ' - Responsible :';
      MyLabel.Caption := dm.GetNextDocNo(MyLabel.Name); // i find next label.caption

     // Above dm.GetNextDocNo  function Increases number in a string) which works well

    // for example if MayLabel.Name ='Label1' then this function makes it 'Label2' so on

//     My problem is in below how can i get Label.Name from Label.Caption ?

// so that I can fill other labels ?

      MyLabel := ???
    end;
    Query1.Next;
  end;

Edited by Henry Olive

Share this post


Link to post
  • Put the labels in an array, and then access them by index, or
  • Use a single label, and include linebreaks in the caption, or
  • Create the labels dynamically, or
  • Probably some other solutions that I have not thought of.
  • Like 1

Share this post


Link to post
Guest

hi @Henry Olive

 

you can try this... the order of each label will be used to "not repeat replacement on before-label".

  • my Panel will be used to restrict my container domain search you see?

image.thumb.png.697376c64374535b43dbfbb5ac11864a.png    image.thumb.png.6b38945425f8212132a0b951f68de46a.png

procedure TForm1.btn_Go_Change_Label_CaptionsClick(Sender: TObject);
var
  lMyArrayRecords: TArray<string>;
  i              : integer;
begin
  // lMyArrayRecords  representing the records returned by your SQL...
  lMyArrayRecords := [                                                                        { }
    'record 1 value', 'record 2 value', 'record 3 value', 'record 4 value', 'record 5 value', { }
  'record 6 value', 'record 7 value', 'record 8 value', 'record 9 value', 'record 10 value'   { }
    ];
  //
  for i := low(lMyArrayRecords) to high(lMyArrayRecords) do
  begin
    if (i > (Panel1.ControlCount - 1)) then
      break; // get out here...
	//
    if (Panel1.Controls[i] is TLabel) then
        TLabel(Panel1.Controls[i]).Caption := lMyArrayRecords[i];
  end;
end;

 

hug

Edited by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×