1. Home
  2. Computing & Technology
  3. Delphi Programming

Overwrite in TMemo and TEdit. Clear all Edit controls on a form

By , About.com Guide

The Windows TEdit and TMemo controls have no overwrite capability. It is possible to emulate this behavior however, by setting the SelLength property of the edit or memo control to one during the processing of the KeyPress event. This causes the character at the current position of the caret to be overwritten.

The following example demonstrates emulation of an overwrite capability of a TMemo component. The state of the overwrite mode can be toggled by pressing the insert key.

Example:

~~~~~~~~~~~~~~~~~~~~~~~~~
type
  TForm1 = class(TForm)
   Memo1: TMemo;
    procedure Memo1KeyDown
    (Sender: TObject; var Key: Word;
    Shift: TShiftState) ;
    procedure Memo1KeyPress
    (Sender: TObject; var Key: Char) ;
   private
     { Private declarations }
     InsertOn : bool;
   public
     { Public declarations }
   end;

var
   Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Memo1KeyDown
(Sender: TObject; var Key: Word; Shift: TShiftState) ;
begin
  if (Key = VK_INSERT) and
     (Shift = []) then
     InsertOn := not InsertOn;
end;

procedure TForm1.Memo1KeyPress
   (Sender: TObject; var Key: Char) ;
begin
  if ((Memo1.SelLength = 0) and
     (not InsertOn)) then
     Memo1.SelLength := 1;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Sometimes we need to clear all the Edit components that are on the form. The task is easy with the following procedure:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure ClearEdits;
  var j : Integer;
begin
for j := 0 to ComponentCount-1 do
   if (Components[j] is TEdit) then
     (Components[j] as TEdit).Text := '';
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Make an application window full screen
« Detecting and preventing Windows shut down

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TMemo, TRichEdit
  6. Overwrite in TMemo and TEdit.

©2009 About.com, a part of The New York Times Company.

All rights reserved.