Board index » delphi » Problem making memo grow with lines of text.

Problem making memo grow with lines of text.

Hi.

I am trying to make the memo control grow and shrink with the width
and # of lines of text entered.   I'm doing ok so far with the width
but the height is another story.

When I press enter after entering text on the first line of the memo
the line of text scrolls up out of site and the memo remains single
line height.  
What I want is that the memo should increase in size by the font
height * 1.   I want the first line of text to allways remain visible
at the top of the memo.

If I have not explained the problem well enough here is the code.

var
 I, CurMaxWidth, CurMaxHeight, LineCount, CurHeight : Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Lines.Clear;
  CurMaxWidth := 0;
  CurMaxHeight := 0;
  CurHeight := 0;
  LineCount := Memo1.Lines.Count;
end;

procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  CurMaxWidth := 0;
  for I := 0 to Memo1.Lines.Count do
    if Canvas.TextWidth(Memo1.Lines.Strings[I]) > CurMaxWidth then
        CurMaxWidth  := Canvas.TextWidth(Memo1.Lines.Strings[I]);

  if Memo1.Lines.Count < 1 then
    CurMaxHeight := 1
  Else
    CurMaxHeight := Memo1.Lines.Count;

  Memo1.Width := CurMaxWidth + Canvas.TextWidth('W');
  Memo1.Height := CurMaxHeight * Canvas.TextHeight('y');
end;

procedure TForm1.Memo1Enter(Sender: TObject);
begin
  if Memo1.Lines.Count < 1 then
    CurMaxHeight := 1
  Else
    CurMaxHeight := Memo1.Lines.Count;

  Memo1.Width := CurMaxWidth + Canvas.TextWidth('W');
  Memo1.Height := CurMaxHeight * Canvas.TextHeight('y');
end;

TIA
Darin

 

Re:Problem making memo grow with lines of text.


Quote
Darin E. Sease wrote:

> Hi.

> I am trying to make the memo control grow and shrink with the width
> and # of lines of text entered.   I'm doing ok so far with the width
> but the height is another story.

> When I press enter after entering text on the first line of the memo
> the line of text scrolls up out of site and the memo remains single
> line height.
> What I want is that the memo should increase in size by the font
> height * 1.   I want the first line of text to allways remain visible
> at the top of the memo.
> TIA
> Darin

I think you will need to override some stuff in the memo VCL, and
possibly do an API call to find the height which would be required for
the new text. Cannot remember which one it is right now, but if you look
through the API stuff for TextOut or some such, you will find that it is
possible to do a call which just returns the required rectangle.
Note that you will need to limit the maximum visible height of the memo.
Dave Bolt/UK

Other Threads