Solved

TMemo vertical scrollbars on demand

Posted on 2002-07-16
8
2,265 Views
Last Modified: 2010-05-18
I have my Tmemo Scrollbars set to ssVertical (WordWrap is also True). The scrollbars show even if they are not necessary. I can do something like

  if Memo1.Lines.Count > x then
    Memo1.ScrollBars := ssVertical
  else
    Memo1.ScrollBars := ssNone;

however I would like a solution that does not rely on me hardcoding the number of lines that can be seen in the TMemo without a scrollbar (x).

Any ideas?

Thanks, Tom.
0
Comment
Question by:tomcorcoran
[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
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7157448
use a trichedit instead
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7158115
Try this:

procedure TForm1.Memo1Change(Sender: TObject);
var line,FontHeight:integer;
    TM:TextMetric;
    win:HDC;
begin
  line:=SendMessage(memo1.handle,EM_lineFromchar,-1,0);// what line is this?
  Win:=GetDC(memo1.handle); //assign a DC
  getTextMetrics(Win,tm);
  FontHeight:=tm.tmHeight; // get font height
  ReleaseDC(memo1.handle,win); //release the device context
  if (line * FontHeight) > memo1.height then  // compare to windowed control
  begin
     memo1.ScrollBars:=SSVertical;
     memo1.selstart:=length(memo1.text);
  end
  else
    Memo1.ScrollBars:=ssnone;
end;



Good luck!!
0
 
LVL 6

Accepted Solution

by:
DrDelphi earned 50 total points
ID: 7158344
a slight modification:

line:=SendMessage(memo1.handle,EM_lineFromchar,length(memo1.Text),0);

instead of:

line:=SendMessage(memo1.handle,EM_lineFromchar,-1,0);// what line is this?



This way you can position the cursor anywhere in the control and still get the scrollbars as needed.


Carry on.....<g>





0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Slick812
ID: 7158675
hello tomcorcoran, you might can use some API messages to get this effect


procedure TForm1.FormCreate(Sender: TObject);
begin
ShowScrollBar(Memo1.Handle, SB_VERT, False);
BarVisible := False;
end;


procedure TForm1.Memo1Change(Sender: TObject);
begin
if not BarVisible then
if SendMessage(Memo1.Handle, EM_GETFIRSTVISIBLELINE,0,0) <> 0 then
  begin
  ShowScrollBar(Memo1.Handle, SB_VERT, True);
  BarVisible := True;
  end;
end;

- - - - - - - - - - - - - - - - - - - - - - -
I'm not sure why you would want this effect, most users won't notice this at all.
0
 

Author Comment

by:tomcorcoran
ID: 7159218
Superb stuff!! Beautiful.

Works a treat. I wonder why this is not default behaviour.

Thanks a million.

Tom.
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 7160439
Cheers.
0
 

Author Comment

by:tomcorcoran
ID: 7483447
I was just looking at this again. It doesn;t worrk quite right. I have the below code :

procedure TForm1.CheckVertScrollNecessary(Memo : TMemo);
  var
    Line       : integer;
    FontHeight : integer;
    TM         : TextMetric;
    win        : HDC;
begin
  Line := SendMessage(Memo.Handle,EM_lineFromchar,Length(Memo.Text),0);
  Win  := GetDC(Memo.Handle);   // assign a DC
  try
    GetTextMetrics(Win,TM);
    FontHeight := TM.TmHeight;  // get font height
  finally
    ReleaseDC(Memo.Handle,Win); // release the device context
  end;

  if (Line * FontHeight) > Memo.Height then begin  // compare to windowed control
    Memo.ScrollBars := ssVertical;
    Memo.SelStart   := Length(Memo.Text);
  end else begin
    Memo.ScrollBars := ssNone;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
  var
    i : integer;
begin
  for i := 0 to 10 do begin
    Memo1.Lines.Add(IntToStr(i))
  end;
  CheckVertScrollNecessary(Memo1);
end;

If this is run on a memo whose height is 150, it adds a vertical scroll unnecessarily. Do you know what the problem might be?

Thanks a lot,

Tom.
0
 

Author Comment

by:tomcorcoran
ID: 7790304
It works fine when I get the FontHeight with
TM.tmAscent (above the line)

Cheers, Tom.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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