Mirror

How to reposition the cursor in a TEdit (Views: 713)


Problem/Question/Abstract:

How to reposition the cursor in a TEdit

Answer:

The example below uses two TEdit's:

unit Cursor;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Edit1Change(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
    CurPos: integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Edit1Change(Sender: TObject);
begin
  CurPos := Edit1.SelStart;
  edit2.Text := IntToStr(CurPos);
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_LEFT then
    dec(CurPos);
  if Key = VK_RIGHT then
    inc(CurPos); {Right Arrow}
  edit2.text := inttostr(CurPos);
end;

end.

<< Back to main page