1. Computing
TMemoBar - TMemo extender
Full source code of the MemoBar Delphi component. MemoBar can be attached to a T(Custom)Memo component to provide additional info/functionality for a Memo component.
 More of this Feature
• TMemoBar SOURCE CODE
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• TMemo Tips and Tricks
• Free source components
• Building custom components

Most editors provide a status-bar like area where, while you type the text, the current location (row and column) of the caret (textual cursor) is displayed as well as whether the editor is in the insert or overwrite mode. Some advanced editors even include the option to "jump" to a certain line number.

Take a look at the Delphi Code Editor. You use the Code editor every day to view and modify your source code. The bottom of the Code Editor includes status bar where the current row and column as well as the edit mode is displayed:

Delphi Code Editor Status Area

StatusBar for a TMemo - TMemoBar
In Delphi application, where entering some textual data is required, a TMemo, TDBMemo or TRichEdit can be used to provide an area for a user to enter / edit lines of text.

If you are looking for a way to display additional info while a user is entering text in a Memo like control, the TMemoBar is what you need.

TMemoBar in Object Inspector

The TMemoBar custom component is designed to add the following features to a TCustomMemo descedant control to (dynamically) display / enable:

  • Position of the caret (textual cursor) and the total number of characters
  • Current row (line) and column number
  • Text entry mode: "insert" or "overwrite".
  • A "Go To Line" dialog window

TMemoBar test project
A TMemoBar is a TPanel descendant with 3 new properties. Most important the "Memo" property (of the TCustomMemo type) is used to assign a TMemo (or TDBMemo, TRichEdit, etc.) component it will provide info about.

with MemoBar1 do
begin
  Memo := Memo1;
  InsertState := isOverwrite; 
  OverwriteCaretHeight := 20;
  OverwriteCaretWidth := 20;
end;

The "OverwriteCaretWidth" and "OverwriteCaretHeight" specify dimensions of the caret when the assigned Memo is in the "overwrite" entry mode. By design, the TMemo control does not provide a way to change entry mode from insert to overwrite (it is always insert) but, the TMemoBar adds this feature to a Memo. Pressing the [Insert] key, toggles between insert and overwrite. When in overwrite mode, a caret is created to visually represent the overwrite mode.

TMemoBar in action

While the user is typing the text, the MemoBar will display the current row and column - position of the text cursor. A user can click this area and call the "GoTo Line" window. The Go to Line enables positioning the cursor on a specific line/row number.

MemoBar's GoTo Line window

Download MemoBar test application.

MemoBar stripped
The MemoBar is a great example of sub classing the window procedure of a control. MemoBar replaces the WindowProc of the assigned Memo in order to handle key presses before they reach the Memo component.

Most of the code handles Windows messages (Memo-specific) like: EM_LINEFROMCHAR, EM_LINEINDEX, EM_SCROLLCARET, etc.

To display the "Go To line" dialog, Delphi's InputQuery function is used.

Installing into the Component palette
First, download the component. The TMemoBar comes as a single unit file (.pas extension). You'll need to add the component into an existing package. Here's "How to Install Custom Component in Delphi (into Existing Package)"
Questions? Comments? Extensions? Exceptions?!
That's it. If you find this component practical and if you extend it by adding more properties, please send your source and make it available to other developers. As always if there are any questions or comments please post them on the Delphi Programming Forum.

©2013 About.com. All rights reserved.