Delphi Programming

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

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

By Zarko Gajic, About.com

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

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  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.