Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to force a TListBox to set a horizontol scrollbar if an entry is being cropp Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
27-Aug-02
Category
VCL-General
Language
Delphi 2.x
Views
92
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I have a listbox on a form which contains a list of stuff of varying widths. In 
some situations, they all fit in the box, in some cases the entries are too long to 
fit and their right end gets cropped. Is there some way to force the listbox to set 
a horizontol scrollbar if and only if an entry is being cropped? I can brute force 
set the ScrollWidth property to 1000, but this puts a scrollbar in place all the 
time. I only want a scrollbar if it's necessary.

Answer:

1   { ... }
2   listbox.Scrollwidth := CalcMaxWidthOfStrings(listbox.Items, listbox.font);
3   { ... }
4   
5   function CalcMaxWidthOfStrings(aList: TStrings; aFont: TFont): Integer;
6   var
7     max, n, i: Integer;
8     canvas: TCanvas;
9   begin
10    Assert(Assigned(aList));
11    Assert(Assigned(aFont));
12    canvas := TCanvas.Create;
13    try
14      canvas.Handle := CreateDC('DISPLAY', nil, nil, nil);
15      try
16        Canvas.Font := aFont;
17        max := 0;
18        for i := 0 to aList.Count - 1 do
19        begin
20          n := Canvas.TextWidth(aList[i]);
21          if n > max then
22            max := n;
23        end;
24        Result := max;
25      finally
26        DeleteDC(canvas.Handle);
27        canvas.Handle := 0;
28      end;
29    finally
30      canvas.free;
31    end;
32  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC