delphi 中如何利用一个按钮控制标签的显示和隐藏 ,按钮的Caption如何在运行时自动转换为显示或隐藏

2025-02-27 16:37:14
推荐回答(4个)
回答1:

两种办法
①设置一个按钮自动改变文字和隐藏或显示标签
procedure TForm1.Button1Click(Sender: TObject);
begin
if button1.Caption ='显示' then
begin
label1.Visible:=true;
button1.Caption:='隐藏'
end else
begin
label1.Visible:=false;
button1.Caption:= '显示';
end;
end;
②设置俩个按钮,俩按钮重合!
按钮1的代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Visible:=true;
button2.BringToFront;
button1.Caption:='显示';
end;
按钮2的代码:
procedure TForm1.Button2Click(Sender: TObject);
begin
label1.Visible:=false;
button1.BringToFront;
button2.Caption:='隐藏';
end;
这样按钮1和按钮2就互相转换,标签就会自动隐藏和显示!!
如果你是新手的话,你用第一个比较方便,第二个不怎么好用..

回答2:

标签的显示和隐藏由标签的Visible属性控制 true为显示
不可能隐藏按钮的Caption ,只可以将其临时赋值为‘’

回答3:

在onclick实践中加入代码
begin
if button1.caption='' then
button1.caption:='xxxx'//xxx为你原来的caption值
else
button1.caption:='';
end;

回答4:

procedure ButtonOnClick(Sender :XXX);
begin
if TButton(Sender).Caption = '显示' then begin
Label1.Visible := True;
TButton(Sender).Caption := '隐藏';
end else begin
Label1.Visible := False;
TButton(Sender).Caption := '显示';
end;

end;