Recent

Author Topic: How to correctly assign a value to TEdit?  (Read 2748 times)

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 437
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
How to correctly assign a value to TEdit?
« on: July 01, 2024, 11:12:59 pm »
I thought it would be informative to display the total number of Contact records on the main Contact form.  So I attempted to use TEdit but can't assign as per below (EditTTLCntks is the TEdit):

Code: Pascal  [Select][+][-]
  1.   IntRecCount:= DbfCntks.ExactRecordCount;
  2.   Assign(EditTTLCntks, Str(IntRecCount,4))
  3.  

Thaddy

  • Hero Member
  • *****
  • Posts: 18797
  • Glad to be alive.
Re: How to correctly assign a value to TEdit?
« Reply #1 on: July 01, 2024, 11:39:32 pm »
Code: Pascal  [Select][+][-]
  1. writestr(EditTTLCntks.Text, IntRecCount:4);
or
Code: Pascal  [Select][+][-]
  1. EditTTLCntks.Text := IntRecCount.ToString;
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

korba812

  • Sr. Member
  • ****
  • Posts: 483
Re: How to correctly assign a value to TEdit?
« Reply #2 on: July 02, 2024, 12:21:03 am »
Code: Pascal  [Select][+][-]
  1. writestr(EditTTLCntks.Text, IntRecCount:4);
It won't compile because TEdit.Text is a property.

jamie

  • Hero Member
  • *****
  • Posts: 7610
Re: How to correctly assign a value to TEdit?
« Reply #3 on: July 02, 2024, 02:04:31 am »
Code: Pascal  [Select][+][-]
  1. EditTTLCntks.Text := '%4d'.format([IntRecCount]);
  2.  

How about that?
The only true wisdom is knowing you know nothing

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 437
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: How to correctly assign a value to TEdit?
« Reply #4 on: July 02, 2024, 05:19:58 pm »
Awesome!  Where are you guys getting these functions, etc.?  What's a good document that I could download just to learn and reference?

Greatly appreciate all of you Wizards!

NickyTi

  • New Member
  • *
  • Posts: 11
Re: How to correctly assign a value to TEdit?
« Reply #5 on: July 03, 2024, 12:11:31 am »
or use
Code: Pascal  [Select][+][-]
  1. EditTTLCntks.Text := '%.4d'[color=red].[/color]format([IntRecCount]);
for leading zeros

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: How to correctly assign a value to TEdit?
« Reply #6 on: July 03, 2024, 10:21:31 am »
OMG.
Whats wrong with classic syntax like:
Code: Pascal  [Select][+][-]
  1. EditTTLCntks.Text := Format('%.4d',[IntRecCount]);
Is it not sexy enough?

Bart

wp

  • Hero Member
  • *****
  • Posts: 13431
Re: How to correctly assign a value to TEdit?
« Reply #7 on: July 03, 2024, 10:25:29 am »
+1

Or even more old-style:
Code: Pascal  [Select][+][-]
  1. EditTTLCntks.Text := IntToStr(IntRecCount);

BTW, why 4 zero-padded digits ('%.4d')? (Format('%.4d', [1]) --> '0001'). Is this really wanted?
« Last Edit: July 03, 2024, 10:32:07 am by wp »

dseligo

  • Hero Member
  • *****
  • Posts: 1674
Re: How to correctly assign a value to TEdit?
« Reply #8 on: July 03, 2024, 11:33:13 am »
OMG.
:)
To me it looks like Yoda style.

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 437
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: How to correctly assign a value to TEdit?
« Reply #9 on: July 03, 2024, 05:36:41 pm »
FYI, the result assigned to the TEdit from Jamie:

Code: Pascal  [Select][+][-]
  1. EditTTLCntks.Text := '%4d'.format([IntRecCount]);
  2.  

isn't displaying zeros in front of the RecCount for my Clients datafile which only has 21 test records.

wp

  • Hero Member
  • *****
  • Posts: 13431
Re: How to correctly assign a value to TEdit?
« Reply #10 on: July 03, 2024, 05:43:06 pm »
FYI, the result assigned to the TEdit from Jamie:

Code: Pascal  [Select][+][-]
  1. EditTTLCntks.Text := '%4d'.format([IntRecCount]);
  2.  

isn't displaying zeros in front of the RecCount for my Clients datafile which only has 21 test records.
Because '%4d' is not the correct format for leading zeros. It adds leading spaces. For leading zeros you must write '%.4d' (note the '.'). Quite mysterious, I know. But read https://www.freepascal.org/docs-html/rtl/sysutils/format.html.

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: How to correctly assign a value to TEdit?
« Reply #11 on: July 03, 2024, 05:46:01 pm »
Using '%.4d' as the format string will give you leading zero's. Notice the dot (.) in the format string.

FWIW: While '%4d'.format([IntRecCount]) may look sexier than old style syntax, it'll invoke a class helper, so it has more overhead.

If you don't need leading blanks of zero's a simple IntToStr() will suffice (or if you insist on "sexy" syntax: IntRecCount.ToString).

Bart

Khrys

  • Sr. Member
  • ****
  • Posts: 402
Re: How to correctly assign a value to TEdit?
« Reply #12 on: July 04, 2024, 07:29:46 am »
FWIW: While '%4d'.format([IntRecCount]) may look sexier than old style syntax, it'll invoke a class helper, so it has more overhead.

It's even worse than that:

Code: Pascal  [Select][+][-]
  1. function Foo(X: Integer): String;
  2. begin
  3.   Result := Format('%.4d', [X]);
  4. end;
  5.  
  6. function Bar(X: Integer): String;
  7. begin
  8.   Result := '%.4d'.Format([X]);
  9. end;

Even using  -O4, on  x86_64-win64  this compiles to

Code: ASM  [Select][+][-]
  1. Foo:
  2.   lea    rsp,[rsp-0x38]
  3.   mov    rax,rcx
  4.   mov    DWORD PTR [rsp+0x28],edx
  5.   mov    QWORD PTR [rsp+0x20],0x0
  6.   lea    r8,[rsp+0x20]
  7.   mov    rcx,rax
  8.   xor    r9,r9
  9.   lea    rdx,[rip+0x13979]        # 0x100015030 <FPC_RESOURCESTRINGTABLES+48>
  10.   call   0x10000ece0 <SYSUTILS_$$_FORMAT$ANSISTRING$array_of_const$$ANSISTRING>
  11.   nop
  12.   lea    rsp,[rsp+0x38]
  13.   ret
  14.  
  15. Bar:
  16.   push   rbp
  17.   mov    rbp,rsp
  18.   lea    rsp,[rsp-0x50]
  19.   mov    QWORD PTR [rbp-0x28],rbx
  20.   mov    QWORD PTR [rbp-0x20],rsi
  21.   mov    rbx,rcx
  22.   mov    QWORD PTR [rbp-0x18],0x0
  23.   nop
  24.   mov    DWORD PTR [rbp-0x8],edx
  25.   mov    QWORD PTR [rbp-0x10],0x0
  26.   lea    rsi,[rbp-0x10]
  27.   lea    rcx,[rbp-0x18]
  28.   call   0x1000043d0 <fpc_ansistr_decr_ref>
  29.   lea    rdx,[rip+0x13904]        # 0x100015030 <FPC_RESOURCESTRINGTABLES+48>
  30.   lea    rcx,[rbp-0x18]
  31.   call   0x100004450 <fpc_ansistr_assign>
  32.   lea    rcx,[rbp-0x18]
  33.   mov    rdx,rbx
  34.   xor    r9,r9
  35.   mov    r8,rsi
  36.   call   0x10000d7c0 <SYSUTILS$_$TSTRINGHELPER_$__$$_FORMAT$array_of_const$$ANSISTRING>
  37.   nop
  38.   mov    rcx,rbp
  39.   call   0x1000016d0 <fin$00000004>
  40.   mov    rbx,QWORD PTR [rbp-0x28]
  41.   mov    rsi,QWORD PTR [rbp-0x20]
  42.   lea    rsp,[rbp+0x0]
  43.   pop    rbp
  44.   ret
  45.  
  46. fin$00000004:
  47.   push   rbp
  48.   mov    rbp,rcx
  49.   lea    rsp,[rsp-0x20]
  50.   lea    rcx,[rbp-0x18]
  51.   call   0x1000043d0 <fpc_ansistr_decr_ref>
  52.   nop
  53.   lea    rsp,[rsp+0x20]
  54.   pop    rbp
  55.   ret

The method call creates a temporary  AnsiString  instance, and all the implicit exception handling stemming from that...

Rizzz

  • New member
  • *
  • Posts: 7
Re: How to correctly assign a value to TEdit?
« Reply #13 on: July 08, 2024, 11:42:55 am »
Try this simplified version of your code:

Code: PHP  [Select][+][-]
  1. pascal
  2. Copy code
  3. var
  4.   IntRecCount: Integer;
  5. begin
  6.   IntRecCount := DbfCntks.ExactRecordCount;
  7.   EditTTLCntks.Text := IntToStr(IntRecCount);
  8.  

This should assign the record count to the TEdit component correctly.

 

TinyPortal © 2005-2018