Solved

Resize a richedit control

Posted on 2002-07-06
4
1,522 Views
Last Modified: 2013-11-22
I want to resize a richedit control so that all the text in the control is shown. ie as you type in the control it automatically increases/descreses size.

someting like richedit1.Height := round(richedit1.Lines.Count * 13.5);

this dosn't work properly, I need to get the logical client area ?

0
Comment
Question by:julianpointer
  • 3
4 Comments
 
LVL 7

Expert Comment

by:Cynna
ID: 7133713
julianpointer,

Line height is font height+external leading. BUT, you
also have to account for constant offset, consisting of
additional spacing and bevel of top/bottom edges of TRichEdit.

For example (copy/paste):

procedure TForm1.ResizeRichEdit;
var TM : TTextMetric;
    Offset: Integer;
begin
  Offset:=8;
  with RichEdit1 do begin
    GetTextMetrics(Canvas.Handle, TM);
    Height := Lines.Count * (TM.tmHeight + TM.tmExternalLeading) + Offset;
  end;
end;


If you want this to happen automatically, probably the easiest
would be placing it in OnResizeRequest event:

procedure TForm1.RichEdit1ResizeRequest(Sender: TObject; Rect: TRect);
begin
  ResizeRichEdit;
end;
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7133836
Also, if you're bothered with auto-scroll on Enter,
this is improved version that takes care of it:

var OldLineCount: Integer; // global var

(....)

procedure TForm1.ResizeRichEdit(VSize: Integer);
var TM : TTextMetric;
    Offset: Integer;
begin
  Offset:=8;
  with RichEdit1 do begin
    GetTextMetrics(Canvas.Handle, TM);
    Height := VSize * (TM.tmHeight + TM.tmExternalLeading) + Offset;
  end;
end;

procedure TForm1.RichEdit1ResizeRequest(Sender: TObject; Rect: TRect);
// improved event handler
var s: String;
    CurrentLineCount: Integer;
begin
  RichEdit1.OnResizeRequest:=nil;
    s:=RichEdit1.Text;
    if s<>'' then begin
       CurrentLineCount:=RichEdit1.Lines.Count;
       if s[length(s)]=Chr(10) then Inc(CurrentLineCount);
       if (CurrentLineCount<>OldLineCount) then ResizeRichEdit(CurrentLineCount);
       OldLineCount:=CurrentLineCount;
    end;
  RichEdit1.OnResizeRequest:=RichEdit1ResizeRequest;
end;
0
 
LVL 3

Author Comment

by:julianpointer
ID: 7136129
Two Issues,
  1. When you alter the font size.
  2. When you scroll off the bottom of the screen you lose the caret.
0
 
LVL 7

Accepted Solution

by:
Cynna earned 200 total points
ID: 7136493
julianpointer,

> 1. When you alter the font size.

I'm sorry, I didn't do much testing on the code, and I went for a straight-forward,
hammer-approach - a mistake. The code that does much better is ridiculously
simple - in fact, you can find it right in the Delphi Help :). It turns out that Delphi
implements EN_REQUESTRESIZE message, along with the REQRESIZE structure.
Moreover, it's all nicely wrapped up in a OnResizeRequest event - so you
actually have the exact size of the RE control to embend inner text right in
the Rect argument, which makes your problem trivial (if you read Help...:)



> 2. When you scroll off the bottom of the screen you lose the caret.

I'm not sure I follow you on this one. If you mean that RE becomes longer
than your form, than all you have to do is scroll the form down (assuming you
left the default form AutoScroll property on).

OK, this is the code that implements my comments:


procedure TForm2.RichEdit1ResizeRequest(Sender: TObject; Rect: TRect);
begin
   // Keep the RE dimensions wrapped around its contens:
   (Sender as TRichEdit).BoundsRect := Rect;
   // Scroll the form contens down:
   VertScrollBar.Position:=VertScrollBar.Range;
end;
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi component that can load a DLL in design time? 8 64
HTML text in the body of an email (delphi code) 12 122
LAN or WAN ? 11 92
Delphi inherited method 6 70
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question