Solved

TMemo vertical scrollbars on demand

Posted on 2002-07-16
8
2,208 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
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
 
LVL 33

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now