Making a Label Visible/Not Visible in Delphi
I would like a button to change a label between being visible and not visible when clicked. I Tried the following code, but it doesnt work:开发者_开发问答
Var:
Hidden : Boolean;
Begin
If Hidden = True
Then
Begin
Label6.Visible := True;
Hidden := False;
End;
If Hidden = False
Then
Begin
Label6.Visible := False;
Hidden := True;
End;
It compiles, but doesn't work!
Do this:
Label6.Visible := not Label6.Visible;
That's all the code you need.
Also, if you're going going to address the label in code, please give it a proper identifying name (like lblCountOfMatches or something).
Finally, the reason your code is not working is that Hidden is never set. It will default to false when declared. If you want to use the code you have now (which is too verbose) you must issue:
Hidden := Label6.Visible
before inspecting Hidden.
The problem is two-fold: you declared a local variable which you then try to compare and you execute both comparisons even if the first one was processed.
You also don't need the boolean: you can just check if it's currently visible
What you should be doing is therefore something like this:
begin
if Label6.Visible then
Label6.Visible := False
else
Label6.Visible := True;
end;
Or the even simpler:
begin
Label6.Visible := not Label6.Visible;
end;
I had a similar problem. I noticed that this caused because I changed the visibility-state of the control (Tedit) when the Form was not visible yet.
So this did not worked: Form2.Edit1.Visible:=True; Form2.Visible:=True;
This worked: Form2.Visible:=True; Form2.Edit1.Visible:=True;
So first make the Form visible, then the control.
Before finding this I tried many solutions, but none worked. I am using Delphi 2007.
精彩评论