不如这样更实在,省得去绞尽脑汁。 procedure TForm1.Button1Click(Sender: TObject); const LabelCaption = 'Test'; var i :integer; begin for i := 0 to ComponentCount-1 do if Components[i] is TLabel then TLabel(Components[i]).Caption := LabelCaption; end;
procedure TForm1.Button1Click(Sender: TObject); const LabelCaption = 'Test'; var i :integer; begin for i := 0 to ComponentCount-1 do if Components[i] is TLabel then TLabel(Components[i]).Caption := LabelCaption; end; 另外 可以通过sendmessage 的形式触发该事件
procedure TForm1.Button1Click(Sender: TObject);
const
LabelCaption = 'Test';
var
i :integer;
begin
for i := 0 to ComponentCount-1 do
if Components[i] is TLabel then
TLabel(Components[i]).Caption := LabelCaption;
end;
这个需求有一定的特殊性,因为这些label在不同的窗体中,我在每个窗体中定义了消息,然后通过设置全局变量给这些窗体发送消息
帖子过几天结,希望还有高人出现。
TMyLabel=Class(TLabel);
var
ps : PChar;procedure TForm1.FormCreate(Sender: TObject);
begin
ps := StrAlloc(30);
ZeroMemory(ps , 30);
StrCopy(ps , PChar(DateTimeToStr(Now()))); //让Lebel要显示的内容指向了ps这个指针
TMyLabel(Label1).WindowText := ps;
//调整一下,AutoSize等操作
TMyLabel(Label1).AdjustBounds;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
//修改指针处的值
ZeroMemory(ps , 30);
StrCopy(ps , PChar(DateTimeToStr(Now())));
//必须调用这句,或者相关的刷新操作才能马上显示
Label1.Refresh; //或者 Self.Refresh;
end;
因此,还是用消息通知,逐个赋值得了.
可以在Form.OnClose中
用
TMyLabel(Label1).WindowText := NIL;
来去掉,但是通知更新必须的作,
procedure TForm1.Button1Click(Sender: TObject);
const
LabelCaption = 'Test';
var
i :integer;
begin
for i := 0 to ComponentCount-1 do
if Components[i] is TLabel then
TLabel(Components[i]).Caption := LabelCaption;
end;
另外 可以通过sendmessage 的形式触发该事件