Recent

Author Topic: [SOLVED] How do I convert a 'Constant String' or 'Ansi String' to TAlign?  (Read 364 times)

Aruna

  • Full Member
  • ***
  • Posts: 115
I am trying to set the align property of a TMemo component in the Form through code.
When trying to set align for a TMemo I get the errors shown below.

When trying to set it straight from the code like:
Code: Pascal  [Select][+][-]
  1. Memo1.Align:='alRight';

I get the error below.
Code: Pascal  [Select][+][-]
  1. Compile Project, Target: /media/aruna/linux-next/home/DEMO/project1: Exit code 1, Errors: 1, Hints: 1
  2. unit4.pas(36,25) Error: Incompatible type for arg no. 1: Got "Constant String", expected "TAlign"
  3. control.inc(4038,20) Hint: Found declaration: SetAlign(TAlign);

This is trying to set align using a TCcomboBox:
Code: Pascal  [Select][+][-]
  1. Memo1.Align:=combobox1.Items.Strings[combobox1.ItemIndex];

I get the error below.
Code: Pascal  [Select][+][-]
  1. Compile Project, Target: /media/aruna/linux-next/home/DEMO/project1: Exit code 1, Errors: 1, Hints: 1
  2. unit4.pas(38,60) Error: Incompatible type for arg no. 1: Got "AnsiString", expected "TAlign"
  3. control.inc(4038,20) Hint: Found declaration: SetAlign(TAlign);

This works :
Code: Pascal  [Select][+][-]
  1. Memo1.Align:= alRight;

So how do I convert a 'Constant String' or 'Ansi String' to TAlign, please?
Thank you, Aruna
« Last Edit: December 11, 2022, 05:31:23 am by Aruna »

lainz

  • Hero Member
  • *****
  • Posts: 4079
Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
« Reply #1 on: December 02, 2022, 12:29:23 am »
Code: Pascal  [Select][+][-]
  1. function StrToALign(const s: String): TAlign;
  2. begin
  3.   case s of
  4.     'alRight': exit(alRight);
  5.     'alLeft': exit(alLeft);
  6.     'alTop': exit(alTop);
  7.     'alBottom': exit(alBottom);
  8.     'alNone': exit(alNone);
  9.   end;
  10. end;
  11.  
lainz.github.io

dsiders

  • Hero Member
  • *****
  • Posts: 761
Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
« Reply #2 on: December 02, 2022, 01:07:15 am »
Code: Pascal  [Select][+][-]
  1. function StrToALign(const s: String): TAlign;
  2. begin
  3.   case s of
  4.     'alRight': exit(alRight);
  5.     'alLeft': exit(alLeft);
  6.     'alTop': exit(alTop);
  7.     'alBottom': exit(alBottom);
  8.     'alNone': exit(alNone);
  9.   end;
  10. end;
  11.  

Or:

Code: Pascal  [Select][+][-]
  1. uses TypInfo;
  2. // ...
  3. var EnumVal: Integer;
  4.  
  5. EnumVal := GetEnumValue(TypeInfo(TAlignment), 'taRightJustify');
  6. Memo1.Alignment := TAlignment(EnumVal);
Preview Lazarus 2.3.0 documentation at: https://dsiders.gitlab.io/lazdocsnext

Aruna

  • Full Member
  • ***
  • Posts: 115
Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
« Reply #3 on: December 02, 2022, 01:17:01 am »
Code: Pascal  [Select][+][-]
  1. function StrToALign(const s: String): TAlign;
  2. begin
  3.   case s of
  4.     'alRight': exit(alRight);
  5.     'alLeft': exit(alLeft);
  6.     'alTop': exit(alTop);
  7.     'alBottom': exit(alBottom);
  8.     'alNone': exit(alNone);
  9.   end;
  10. end;
  11.  

Works like a charm @lainz Thank you.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 8620
  • Debugger - SynEdit - and more
    • wiki
Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
« Reply #4 on: December 02, 2022, 01:52:05 am »
Code: Pascal  [Select][+][-]
  1. var t: TAlign; s: String;
  2. begin
  3.   s := 'alTop';
  4.   try
  5.     ReadStr(s, t);
  6.   except
  7.     // in case s was 'bad value' ...
  8.   end;
  9.   Memo1.Align := t;
  10. end;

Aruna

  • Full Member
  • ***
  • Posts: 115
Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
« Reply #5 on: December 02, 2022, 02:33:40 am »
Or:

Code: Pascal  [Select][+][-]
  1. uses TypInfo;
  2. // ...
  3. var EnumVal: Integer;
  4.  
  5. EnumVal := GetEnumValue(TypeInfo(TAlignment), 'taRightJustify');
  6. Memo1.Alignment := TAlignment(EnumVal);

Hi @dsiders this works for the actual 'content' in the Tmemo I was actually asking for the 'Align' property but what you have sent has taught me how to justify things in the Tmemo so it is all good. Thank you very much.

Aruna

  • Full Member
  • ***
  • Posts: 115
Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
« Reply #6 on: December 02, 2022, 02:44:43 am »
Code: Pascal  [Select][+][-]
  1. var t: TAlign; s: String;
  2. begin
  3.   s := 'alTop';
  4.   try
  5.     ReadStr(s, t);
  6.   except
  7.     // in case s was 'bad value' ...
  8.   end;
  9.   Memo1.Align := t;
  10. end;

Hi @Martin_fr Yours works like a charm too. Thank you. The more I use Lazarus the more I am beginning to realize the types of applications you can create with Lazarus are limited only by your imagination  :o
« Last Edit: December 02, 2022, 02:54:48 am by Aruna »

 

TinyPortal © 2005-2018