Solved

TMemo vertical scrollbars on demand

Posted on 2002-07-16
8
2,223 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

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…
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…
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…

777 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