Mega Code Archive

 
Categories / Delphi / Examples
 

Justifying edit text

Title: Justifying edit text Question: How can I create a single line edit control that is right or centered justified? Answer: You can use a TMemo component, since the TEdit component does not directly support center and right justification. In addition, you will need to prevent the user from pressing the Enter, Ctrl-Enter, and a variety of arrow key combinations to prevent more than one line to be added to the memo. This can be accomplished by scanning the TMemo's text string for carriage return and line feed characters and deleting them during the TMemo's Change and KeyPress events. Optionally, you could replace any carriage returns with spaces to accommodate a multi-line paste operation. Example: procedure TForm1.FormCreate(Sender: TObject); begin Memo1.Alignment := taRightJustify; Memo1.MaxLength := 24; Memo1.WantReturns := false; Memo1.WordWrap := false; end; procedure MultiLineMemoToSingleLine(Memo : TMemo); var t : string; begin t := Memo.Text; if Pos(#13, t) 0 then begin while Pos(#13, t) 0 do delete(t, Pos(#13, t), 1); while Pos(#10, t) 0 do delete(t, Pos(#10, t), 1); Memo.Text := t; end; end; procedure TForm1.Memo1Change(Sender: TObject); begin MultiLineMemoToSingleLine(Memo1); end; procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char); begin MultiLineMemoToSingleLine(Memo1); end;