Recent

Author Topic: [SOLVED] In some cases Name of TMemo or TEdit appears in text.  (Read 4401 times)

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Lazarus version 1.6.2
Windows version XP

After open new ptoject put this code to Create event of Form:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var mMemo:TMemo;
  3. begin
  4.   mMemo := TMemo.Create(self);
  5.   mMemo.Parent := self;
  6.   mMemo.Text:= '';
  7.   mMemo.Name:= 'MemoName';
  8.   showmessage(mMemo.Text + ', ' + mMemo.Name);
  9. end;  
  10.  
Will appear
Quote
MemoName, MemoName

Same with Tedit and probably with TComponents similar to TMemo and TEdit.

But thus all is Ok;
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var mMemo:TMemo;
  3. begin
  4.   mMemo := TMemo.Create(self);
  5.   mMemo.Parent := self;
  6.   mMemo.Name:= 'MemoName';
  7.   mMemo.Text:= '';
  8.   showmessage(mMemo.Text + ', ' + mMemo.Name);
  9. end;
  10.  
Will appear
Quote
, MemoName

In first case can be delete string number 6.
« Last Edit: April 18, 2017, 01:02:16 am by yurkad »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: In some cases Name of TMemo o TEdit appears in text.
« Reply #1 on: April 14, 2017, 06:21:50 pm »
Controls such as TEdit and TMemo that have csSetCaption included in their ControlStyle property will have their Captions set on creation to their Name.

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: In some cases Name of TMemo o TEdit appears in text.
« Reply #2 on: April 14, 2017, 10:25:13 pm »
Quote
will have their Captions set on creation to their Name

It's understood. But here is some bad thing. The string
Code: Pascal  [Select][+][-]
  1. mMemo.Text:= '';

explicitly and clearly says that text must be empty. For me (and can be for many coders)
it's difficult to understand that this command will be ignored.

This is understandable in the inspector of objects, but here - not.
In inspector of objects when we do not specify value of text, it is understandable (but debatable).
But here we indicate directly value of text and it is not understandable to ignore this command.
It is not usual.

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: In some cases Name of TMemo o TEdit appears in text.
« Reply #3 on: April 14, 2017, 10:45:10 pm »
I cannot comment on your previous post. But it seems to me that the problem is caused by setting the Name of a component created at run time. I never do this, the Name of all my run-time created controls is empty. The Name property is only needed by the Object Inspector. At runtime, there is the variable name to identify the control.

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: In some cases Name of TMemo o TEdit appears in text.
« Reply #4 on: April 14, 2017, 10:52:04 pm »
wp, it is very good. Thanks.
But in my case the logic of this project forces me to find for these controls by the name.

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: In some cases Name of TMemo o TEdit appears in text.
« Reply #5 on: April 14, 2017, 10:58:49 pm »
Then this logics is not good. You have the variable name.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   mMemo.Enabled := false;
  4. end;
  5.  

An example for doing something with all memos: Iterate through all Components[] of the form:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.CountMemos;
  2. var
  3.   n: Integer = 0;
  4.   i: Integer;
  5. begin
  6.   for i:=0 to ComponentCount-1 do
  7.     if Components[i] is TMemo then
  8.       inc(n);
  9.   ShowMessage(IntToStr(n) + ' memos found');
  10. end;  

Or, if you have several run-time-created memos, put them in an array of TMemo, now you can address them using the array index.
Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.    ....
  4.   private
  5.     FMemos: Array of TMemo;
  6.   end;
  7.  
  8. procedure TForm1.Form1Create(Sender: TObject);
  9. var
  10.   i: Integer;
  11.   x: Integer = 0;
  12. begin
  13.   SetLength(FMemos, 3);
  14.   for i:=0 to High(FMemos) do
  15.     FMemos[i] := TMemo.Create(self);
  16.     FMemos[i].Parent := self;
  17.     FMemos[i].Left := x;
  18.     inc(x, FMemos[i].Width);
  19.   end;
« Last Edit: April 14, 2017, 11:13:59 pm by wp »

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: In some cases Name of TMemo o TEdit appears in text.
« Reply #6 on: April 14, 2017, 11:02:14 pm »
There are two or more controls TMemo.
But your code is useful for me. I will use it. Thanks.
« Last Edit: April 14, 2017, 11:05:44 pm by yurkad »

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: [SOLVED] In some cases Name of TMemo o TEdit appears in text.
« Reply #7 on: April 16, 2017, 11:19:15 pm »
You can safely use the Tag property to identify different controls, regardless of name or even type. Most visible controls have this property. You can set it at creation time and change or check it any time during the control's life.

As for your original post, when the control is created, both text and name are emtpy strings. When the name is set, and since the 'previous' name matches the text, the text is changed to be the name as well. As Howardpc said, controls with csSetCaption set in property ControlStyle have that behaviour, both at design and runtime. As per the LCL help (http://lazarus-ccr.sourceforge.net/docs/lcl/controls/tcontrolstyle.html), As long as Name=Text, changing the Name will set the Caption.

So basically there's no bug, the control is doing just what it's designed to do.

Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: [SOLVED] In some cases Name of TMemo o TEdit appears in text.
« Reply #8 on: April 17, 2017, 03:31:37 pm »
jmm72, thank.
You are right. It is not bug.
It is not bug by explanation of howardpc.

Code: Pascal  [Select][+][-]
  1. mMemo.Text:= '';
But each one, who first time see that this line  is not working will think that this is an bug.
« Last Edit: April 18, 2017, 11:05:13 pm by yurkad »

 

TinyPortal © 2005-2018