Solved

Resize a richedit control

Posted on 2002-07-06
4
1,571 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple image collision 13 102
Controlled Assessment GCSE - desperate help needed 4 117
FMX and jaudiotracker playing memory stream 29 103
Firemonkey DbLookupComboBox equivalent ? 2 76
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

740 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