Solved

TMemo vertical scrollbars on demand

Posted on 2002-07-16
8
2,284 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

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

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…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month5 days, 16 hours left to enroll

627 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