Solved

TMemo vertical scrollbars on demand

Posted on 2002-07-16
8
2,192 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

12 Experts available now in Live!

Get 1:1 Help Now