Lazarus

Programming => General => Topic started by: Aruna on December 02, 2022, 12:18:10 am

Title: [SOLVED] How do I convert a 'Constant String' or 'Ansi String' to TAlign?
Post by: Aruna on December 02, 2022, 12:18:10 am
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
Title: Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
Post by: lainz 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.  
Title: Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
Post by: dsiders 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);
Title: Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
Post by: Aruna 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.
Title: Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
Post by: Martin_fr 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;
Title: Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
Post by: Aruna 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.
Title: Re: How do I convert a 'Constant String' or 'Ansi String' to TAlign?
Post by: Aruna 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
TinyPortal © 2005-2018