RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomListBox.ItemRect Method

Returns the rectangle that surrounds the item specified in the Item parameter.

Pascal
function ItemRect(Index: Integer): TRect;
C++
__fastcall TRect ItemRect(int Index);

Use ItemRect to get the coordinates of a particular item in the list box. For example, ItemRect is used internally by the ItemAtPos method.  

The Item parameter is the ItemIndex of the item whose position is queried.  

C++ Examples: 

 

/*
This example uses a list box on a form. When the user clicks
one of the items in the list box, a beep occurs if the mouse
is in the first half of the item. A similar technique can be
used to detect "hot" regions in an owner-draw list box:
*/
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  ListBox1->Items->Add("Hello");
  ListBox1->Items->Add("New");
  ListBox1->Items->Add("World");
}

void __fastcall TForm1::ListBox1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  TRect ItemBounds;
  if (Button == mbLeft)
  {
    ItemBounds = ListBox1->ItemRect(ListBox1->ItemIndex);
    if (Y > ItemBounds.Top && 
        Y < ItemBounds.Bottom && 
        X > ItemBounds.Left && 
        X < (ItemBounds.Left + ItemBounds.Right)/2)
      MessageBeep(0);
  }
}

 

Delphi Examples: 

{
This example uses a list box on a form. When the user clicks
one of the items in the list box, a beep occurs if the mouse
is in the first half of the item. A similar technique can be
used to detect "hot" regions in an owner-draw list box:
} 
procedure TForm1.FormCreate(Sender: TObject);
begin
  with ListBox1 do
  begin
    Items.Add('Hello');
    Items.Add('New');
    Items.Add('World');
  end;
end;

procedure TForm1.ListBox1MouseUp (Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  ListBoxItem: TRect;
begin
  if Button = mbLeft then
  begin
    ListBoxItem := ListBox1.ItemRect(ListBox1.ItemIndex);
    if (Y > ListBoxItem.Top) and 
       (Y < ListBoxItem.Bottom) and 
       (X > ListBoxItem.Left) and 
       (X < (ListBoxItem.Left + ListBoxItem.Right) div 2) then
      Beep;
  end;
 end;

 

ItemAtPos 

ItemIndex

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!