Solved

Delphi - count visible lines on TListBox

Posted on 2011-02-23
5
1,321 Views
Last Modified: 2012-06-27
How can I count how much lines are visible in the same time on specific listbox. The user have the ability to change the font size, so I need to know how many lines are visible in any font size.
0
Comment
Question by:oferlll
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 34960117
Try this...
function TListBox.VisibleLines: Integer;
begin
  Result := Height div (Abs(Self.Font.Height) + 2);
end;

Open in new window

0
 
LVL 8

Expert Comment

by:BdLm
ID: 34960128
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function ListBoxItemsVisible( aListBox : TListBox ) : Integer;
begin

     result := round (aListBox.ClientHeight /  aListBox.ItemHeight) ;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
       edit1.Text := IntToStr( ListBoxItemsVisible(ListBox1));
end;

end
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 34960138
You can divide the ListBox.Height over the ListBox.ItemHeight.
You can write the following code in a Button onclick event:
Showmessage(IntToStr(ListBox1.Height Div ListBox1.ItemHeight));

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 34960154
Demo project...
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
    function VisibleLines(AListBox:TListBox): Integer;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.VisibleLines(AListBox:TListBox): Integer;
begin
  Result := AListBox.Height div (Abs(AListBox.Font.Height) + 2);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(inttostr(VisibleLines(ListBox1)));
end;

end.

Open in new window

0
 
LVL 8

Expert Comment

by:BdLm
ID: 34960164
a bit spice it up:

function ListBoxItemsVisible( aListBox : TListBox ) : Integer;
var    MaxItems   : Integer ;
begin

     MaxItems := aListBox.Items.Count;

     result := round (aListBox.ClientHeight /  aListBox.ItemHeight) ;

     
     if result > MaxItems then result := MaxItems;   //  if ListBox not full, report the count of items inside the box


end;
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 125
Correct Component for Shopping Cart. 2 112
Making delphi communicate with a c# service 16 111
scroll down TListBox component in Delphi 1 19
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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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