Recent

Author Topic: Label.Caption not changing  (Read 12017 times)

scons

  • Full Member
  • ***
  • Posts: 141
Label.Caption not changing
« on: August 24, 2016, 12:14:04 am »
Hello,

I have an issue with a Label.Caption:

  • it does not show the right text
  • it is not visible every other clicked file

Small background of the application, I drag files into a Listbox, when I select one of them (onclick event) I want to read the textfile and show some data from specific line(s). When there is no file selected, I the labels must be invisible.

The ShowMessage work perfectly and shows the right data, wich should be in the Label.Caption, The Label.Caption displays the original Caption entered in the IDE.

This is the code I have:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4.   NCList: TStringList;
  5. begin
  6.     i := ListBox1.ItemIndex;
  7.     if i < 0 then
  8.     Exit; // -1 means there is no selected item
  9.     ProfileLabel.Visible := not ProfileLabel.Visible;
  10.     NCList := TStringList.Create;
  11.    try
  12.     NCList.LoadFromFile(ListBox1.Items[i]);
  13.     if NCList.Count = 8 then
  14.        ProfileLabel.Caption := NCList[8];
  15.        Showmessage(NCList[8]);
  16.    finally
  17.     NCList.Free;
  18.    end;
  19. end;

What am I doing wrong ?

Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Label.Caption not changing
« Reply #1 on: August 24, 2016, 12:21:37 am »
Quote
What am I doing wrong ?
If no item is selected you exit the routine and you _always_ invert the visibility of the label once an item is selected ?

scons

  • Full Member
  • ***
  • Posts: 141
Re: Label.Caption not changing
« Reply #2 on: August 24, 2016, 12:32:39 am »
I can set it to always visible yes, for now that would be oke, but why is the label not changing ?
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Label.Caption not changing
« Reply #3 on: August 24, 2016, 12:36:39 am »
in principle the same answer.

Code: [Select]
    ....
    if i < 0 then
    begin
      ProfileLabel.Caption := '';
      ProfileLabel.Visible := false;
      Exit; // -1 means there is no selected item
    end;

    ProfileLabel.Visible := true;
    ...

That should do the trick. I'm not sure if a label auto-updates itself automatically for each widgetset though.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Label.Caption not changing
« Reply #4 on: August 24, 2016, 12:37:44 am »
Code: Pascal  [Select][+][-]
  1.     if NCList.Count = 8 then
  2.        ProfileLabel.Caption := NCList[8];
  3.        Showmessage(NCList[8]);

should be, adding a proper begin end

Code: Pascal  [Select][+][-]
  1.     if NCList.Count = 8 then
  2. begin
  3.        ProfileLabel.Caption := NCList[8];
  4.        Showmessage(NCList[8]);
  5. end

and it or shows both or nothing

scons

  • Full Member
  • ***
  • Posts: 141
Re: Label.Caption not changing
« Reply #5 on: August 24, 2016, 12:52:49 am »

Code: [Select]
    ....
    if i < 0 then
    begin
      ProfileLabel.Caption := '';
      ProfileLabel.Visible := false;
      Exit; // -1 means there is no selected item
    end;

    ProfileLabel.Visible := true;
    ...


Thanks Molly, this works
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

scons

  • Full Member
  • ***
  • Posts: 141
Re: Label.Caption not changing
« Reply #6 on: August 24, 2016, 01:11:30 am »
Code: Pascal  [Select][+][-]
  1.     if NCList.Count = 8 then
  2.        ProfileLabel.Caption := NCList[8];
  3.        Showmessage(NCList[8]);

should be, adding a proper begin end

Code: Pascal  [Select][+][-]
  1.     if NCList.Count = 8 then
  2. begin
  3.        ProfileLabel.Caption := NCList[8];
  4.        Showmessage(NCList[8]);
  5. end

and it or shows both or nothing

No succes, it still shows the ShowMessage, but the Label.Caption does not change.
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

guest58172

  • Guest
Re: Label.Caption not changing
« Reply #7 on: August 24, 2016, 01:20:21 am »
NCList[8] is out of range. The 8th element is NCList[7]

scons

  • Full Member
  • ***
  • Posts: 141
Re: Label.Caption not changing
« Reply #8 on: August 24, 2016, 01:27:07 am »
nono, I want to see line nr 9, even the ShowMessage comes up with the exact line (which should come up in the Label.Caption, I've put the ShowMessage as a extra checkpoint).
De-activating the ShowMessage with // does not change anything.
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

guest58172

  • Guest
Re: Label.Caption not changing
« Reply #9 on: August 24, 2016, 01:32:21 am »
But if NCList.Count = 8 is true then NCList[8] is out of range god damnit.

scons

  • Full Member
  • ***
  • Posts: 141
Re: Label.Caption not changing
« Reply #10 on: August 24, 2016, 01:58:15 am »
If I add a writeln before the Label.Caption it works ??? The Label.Caption changes on the fly.

Why's that ?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4.   NCList: TStringList;
  5. begin
  6.   i := ListBox1.ItemIndex;
  7.   if i < 0 then
  8.   begin
  9.     ProfileLabel.Caption := '';
  10.     ProfileLabel.Visible := False;
  11.     Exit;       // -1 means there is no selected item
  12.   end;
  13.   try
  14.   ProfileLabel.Visible := True;
  15.   NCList := TStringList.Create;
  16.   NCList.LoadFromFile(ListBox1.Items[i]);
  17.   if NCList.Count = 8 then
  18.     writeln (NCList[8]);
  19.     ProfileLabel.Caption := NCList[8];
  20.     //ShowMessage(NCList[8]);
  21.   finally
  22.   NCList.Free;
  23.   end;
  24. end;
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Label.Caption not changing
« Reply #11 on: August 24, 2016, 02:04:01 am »
If I add a writeln before the Label.Caption it works ??? The Label.Caption changes on the fly.

Why's that ?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1Click(Sender: TObject);
  2. var
  3.   i: integer;
  4.   NCList: TStringList;
  5. begin
  6.   i := ListBox1.ItemIndex;
  7.   if i < 0 then
  8.   begin
  9.     ProfileLabel.Caption := '';
  10.     ProfileLabel.Visible := False;
  11.     Exit;       // -1 means there is no selected item
  12.   end;
  13.   try
  14.   ProfileLabel.Visible := True;
  15.   NCList := TStringList.Create;
  16.   NCList.LoadFromFile(ListBox1.Items[i]);
  17.   if NCList.Count = 8 then
  18.     writeln (NCList[8]);
  19.     ProfileLabel.Caption := NCList[8];
  20.     //ShowMessage(NCList[8]);
  21.   finally
  22.   NCList.Free;
  23.   end;
  24. end;

For the same reason i tell you in my previous post. Missing begin end.

guest58172

  • Guest
Re: Label.Caption not changing
« Reply #12 on: August 24, 2016, 03:10:21 am »
Try
Code: Pascal  [Select][+][-]
  1. if NCList.Count = 9 then
  2. begin
  3.   writeln (NCList[8]); // 9th element
  4.   ProfileLabel.Caption := NCList[8];
  5. end;

scons

  • Full Member
  • ***
  • Posts: 141
Re: Label.Caption not changing
« Reply #13 on: August 24, 2016, 03:18:48 am »
I thank you guys for looking into this issue, but if I add a begin + end AFTER the second IF, it just does not work.
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Label.Caption not changing
« Reply #14 on: August 24, 2016, 03:27:23 am »
I thank you guys for looking into this issue, but if I add a begin + end AFTER the second IF, it just does not work.

So you must ensure how much is the count. Its ok that doesnt works because the condition doesnt return true with these numbers.

Try the code of BBasile but change the first line to

If NCList.Count >= 9 then
...

This means there are 9 lines or more in the file. Remember 9 or more. If the file has less lines it will not show nothing.
« Last Edit: August 24, 2016, 03:34:17 am by lainz »

 

TinyPortal © 2005-2018