Search code examples
delphilabeldelphi-7visibilityvisible

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!


Solution

  • 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.