Setting Text Alignment

From RAD Studio
Jump to: navigation, search

Go Up to Working with Text in Controls

In a rich edit or memo component, text can be left- or right-aligned or centered. To change text alignment, set the edit component's Alignment property. Alignment takes effect only if the WordWrap property is True; if word wrapping is turned off, there is no margin to align to.

For example, the following code attaches an OnClick event handler to a Character > Left menu item, then attaches the same event handler to both a Character > Right and Character > Center menu item.

 procedure TForm.AlignClick(Sender: TObject);
 begin
   Left1.Checked := False;  { clear all three checks }
   Right1.Checked := False;
   Center1.Checked := False;
   with Sender as TMenuItem do Checked := True;  { check the item clicked }
   with Editor do  { then set Alignment to match }
     if Left1.Checked then
       Alignment := taLeftJustify
     else if Right1.Checked then
       Alignment := taRightJustify
     else if Center1.Checked then
       Alignment := taCenter;
 end;
switch ((int)RichEdit1->Paragraph->Alignment) {
case 0:
	LeftAlign->Down = true;
	break;

case 1:
	RightAlign->Down = true;
	break;

case 2:
	CenterAlign->Down = true;
	break;
}

You can also use the HMargin property to adjust the left and right margins in a memo control.

See Also