unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Menus, StdCtrls, Buttons; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } arr : array [1..30] of TBitBtn; procedure BitButtonClick(Sender: TObject); public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.BitButtonClick(Sender: TObject); begin if TBitBtn(Sender).Font.Style = [] then begin TBitBtn(Sender).Font.Style := [fsBold]; TBitBtn(Sender).Font.Color := clRed; end else begin TBitBtn(Sender).Font.Style := []; TBitBtn(Sender).Font.Color := clBlack; end; end; procedure TForm1.FormCreate(Sender: TObject); var x, y, f : integer; begin x := 20; y := 20; for f := 1 to 30 do begin arr [f] := TBitBtn.Create (Form1); if arr [f] <> nil then begin arr [f].Parent := Form1; arr [f].left := x; arr [f].top := y; arr [f].width := 35; arr [f].Height := 25; arr [f].caption := IntToStr (f); arr [f].tag := f; arr [f].OnClick := BitButtonClick; inc (x, arr [f].width + 20); if (x + arr [f].width) > form1.width then begin x := 20; inc (y, arr [f].height + 20); end; end; end; end; end. |
Я подразумеваю, что существует два способа:
var lab: TLabel; begin lab := TLabel(FindComponent('Label1')); lab.Caption := 'Эй!' end; |
procedure TForm1.Button2Click(Sender: TObject); var i: smallint; begin for i := 0 to ComponentCount-1 do if (Components[i] is TLabel) AND (Components[i].Tag = 3) then begin TLabel(Components[i]).Caption := 'Это третий!'; break; end; end; |
Как насчет цикла, в котором вы пройдетесь по всем вашим компонентам (надеюсь, Вы знакомы со свойством вашей формы Components)? Это можно сделать приблизительно так:
var I: Integer; begin for I := 0 to ComponentCount -1 do if Components[I] is TLabel then TLabel(Components[I]).Caption := 'Это он!'; end; |
Поместите метки в TList. Затем вы можете сделать приблизительно так:
TLabel(mylist.items[MyValue]).Caption := 'Это оно!'; |
function LabelCaption(MyValue: Integer; Text: string) : Boolean; function LabelCaption(MyValue: Integer; Text: string) : Boolean; var LabelCount, Loop: Integer; begin Result := False; LabelCount := 0; for Loop := 0 to ComponentCount - 1 do begin if Components[Loop] is TLabel then { это метка ? } begin INC(LabelCount); if LabelCount = MyValue then { соответствует индекс моему значению ? } begin Component[Loop].Caption := Text; Result := True; end; end; end; end; |
Вызвать это можно следующим образом:
if LabelCaption(10, 'Это я!') then WriteLn('Мы нашли это!'); |
Другой метод, который более соответствует тому, что вы хотите, заключается в использовании StringList с функциями сортировки, который позволяет найти вашу метку в списке функцией IndexOf. Для добавления имен меток в StringList можно воспользоваться приведенным выше циклом.
Поиск можно выполнять следующим образом:
MyLabels[MyLabels.IndexOf(Label10)].Caption := 'Еще один!'; |
[001636]