?
Solved

Delphi - count visible lines on TListBox

Posted on 2011-02-23
5
Medium Priority
?
1,397 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
[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
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

752 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