Recent

Author Topic: Label Caption Problem  (Read 14346 times)

Zath

  • Sr. Member
  • ****
  • Posts: 391
Label Caption Problem
« on: July 24, 2014, 12:52:57 am »
Can anyone tell me why I can't change the caption on a label ?

Label6.Caption:='Blah Blah';

This is doing my brain in !   >:(

Thanks


Lazarus 1.2.4 r45510 FPC 2.6.4 i386-win32-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Label Caption Problem
« Reply #1 on: July 24, 2014, 12:56:01 am »
Not enough info. Be clear where/when you change it. Post a small, compilable project that demonstrates the behavior.

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Label Caption Problem
« Reply #2 on: July 24, 2014, 01:44:40 am »
I've cut my prog down to the bones to show you and it's now a bit messy but the problem still shows.
Running the prog gives a button to press and it opens a dialog to select a directory. Any will do. That will have written a harmless ini file (RubyEditor.ini) to the D Drive.
Label6 isn't showing.

Closing and restarting the prog will now have Label6.caption showing the previously selected directory.

Why doesn't it show the Label6.Caption:=('No Directory Found');  in FormCreate work ?
I've tried with brackets and without. I've tried setting a string outside and using a variable. All to no avail.
No doubt its a stupid mistake on my part.

Apologies if I've not put this code up right, not really sure how to do it.


Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  EditBtn, ExtCtrls, Menus, Inifiles;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button4: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label6: TLabel;
    Panel3: TPanel;
    SelectDirectoryDialog1: TSelectDirectoryDialog;
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);

  private
    { private declarations }
  public
    { public declarations }
    end;

var
  Form1: TForm1;
  fnName, drName, drPath: string;
implementation

{$R *.lfm}

{ TForm1 }


procedure savetoini(const aFileName:string);
var
vIni : TIniFile;
  begin
  vIni := TInifile.Create('D:\RubyEditor.ini');
  try
    vIni.writestring('Main','CurrentPath',drName);
  finally
    vIni.free;
  end;
end;

procedure readfromini(const aFileName:string);
var
vIni : TIniFile;
  begin
  vIni := TInifile.Create('D:\RubyEditor.ini');
  try
    vIni.readstring('Main','CurrentPath','');
  finally
    drName:=vIni.ReadString('Main','CurrentPath','');
    vIni.free;
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  if SelectDirectoryDialog1.Execute then begin
    drName := SelectDirectoryDialog1.FileName;
    edit1.text:=drName;
    savetoini('D:\RubyEditor.ini');
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
readfromini('D:\RubyEditor.ini');
if drName='' then Begin
  Label6.Caption:=('No Directory Found');
end;
Label6.Caption:=drName;
edit2.text:=drName;
end;


end.
     
« Last Edit: July 24, 2014, 01:46:47 am by Zath »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Label Caption Problem
« Reply #3 on: July 24, 2014, 02:53:12 am »
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
readfromini('D:\RubyEditor.ini');
if drName='' then Begin
  Label6.Caption:=('No Directory Found');
end
else Label6.Caption:=drName;
edit2.text:=drName;
end;

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Rails

  • Guest
Re: Label Caption Problem
« Reply #4 on: July 24, 2014, 02:57:18 am »
This doesn't make any sense.

Code: [Select]
if drName='' then Begin
  Label6.Caption:=('No Directory Found');
end;
Label6.Caption:=drName;  // This line overrides the previous IF statement

Did you mean to do this?

Code: [Select]
if drName='' then Begin
  Label6.Caption:=('No Directory Found');
end else
Label6.Caption:=drName;


Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Label Caption Problem
« Reply #5 on: July 24, 2014, 10:44:36 am »
Taazz and Rails, you have both amended the IF statement to include Else and removed the semicolon I have on the first end.
I'll try this when I get home although my instinct tells me this won't effect the preceeding part of assigning the text to the caption and displaying it.

I hope I'm proved wrong  :)

Rails

  • Guest
Re: Label Caption Problem
« Reply #6 on: July 24, 2014, 11:51:53 am »
Taazz and Rails, you have both amended the IF statement to include Else and removed the semicolon I have on the first end.
I'll try this when I get home although my instinct tells me this won't effect the preceeding part of assigning the text to the caption and displaying it.

I hope I'm proved wrong  :)

It doesn't matter what text you assign to the caption if the following statement assigns something different. Those statements execute in microseconds or less. Even if the display were to paint the first assignment, your eye couldn't follow it. It is overwritten immediately effectively negating the if completely. The else is required. Take a look at the following.

https://en.wikipedia.org/wiki/Conditional_(computer_programming)

The semicolon is removed in order to meet Pascal's syntax requirements.

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Label Caption Problem
« Reply #7 on: July 24, 2014, 01:42:05 pm »
Taazz and Rails, you have both amended the IF statement to include Else and removed the semicolon I have on the first end.
I'll try this when I get home although my instinct tells me this won't effect the preceeding part of assigning the text to the caption and displaying it.

I hope I'm proved wrong  :)

It doesn't matter what text you assign to the caption if the following statement assigns something different. Those statements execute in microseconds or less. Even if the display were to paint the first assignment, your eye couldn't follow it. It is overwritten immediately effectively negating the if completely. The else is required. Take a look at the following.

https://en.wikipedia.org/wiki/Conditional_(computer_programming)

The semicolon is removed in order to meet Pascal's syntax requirements.

Following on from that, because drName equals  ' ', it's showing as blank.
Yes I can see that now.

Thanks for your time.

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Label Caption Problem
« Reply #8 on: July 24, 2014, 10:42:17 pm »
Ok, thanks everyone.
It was a basic error that slapped me in the face.

Thanks for pointing it out, all is working.





 

TinyPortal © 2005-2018