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!

Answers


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.


Need Your Help

MySql Syntax error: You have an error in your SQL syntax near '' at line 1

c# mysql sql syntax-error

EDIT: Apologies for the stupid question. My own lapse in concentration, plus the fact that I'm still just learning the SQL syntax kinda caused me to miss the issue.

How can I instruct Perforce to merge instead of overwrite or revert when unshelving a file?

perforce shelving unshelve

How can I instruct Perforce to merge changes into an existing, open, and modified file in the workspace when unshelving a file shelved in the depot? The only options that Perforce appears to offer...