Solved

Delphi - count visible lines on TListBox

Posted on 2011-02-23
5
1,265 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

11 Experts available now in Live!

Get 1:1 Help Now