Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

TcxMCListBox.ItemAtPos(TPoint,Boolean) Method

Returns the index of the item located under the specified point.

#Declaration

Delphi
function ItemAtPos(const APos: TPoint; AExisting: Boolean): Integer;

#Parameters

Name Type
APos TPoint
AExisting Boolean

#Returns

Type
Integer

#Remarks

Use the ItemAtPos method to determine the item located under the specified point. The test point is specified by the APos parameter. This parameter must specify the point in the control’s client coordinates (relatively to its top-left corner). If you have screen coordinates, use the ScreenToClient method of the control to obtain the control’s client coordinates.

The AExisting parameter value affects the return value of the ItemAtPos method. If True, the method will return -1 if the specified point is within the control but below the last item. Otherwise, the method will return the index of the last item in such cases.

Most commonly, the ItemAtPos method is used to determine the item located under the mouse cursor. The following sample code shows how this can be used. It handles the OnMouseDown event to select the right-clicked item and invoke the popup menu. The popup menu must contain a single item that removes selected items from the control. Below are the handlers of the menu item’s OnClick and list box’s OnMouseDown events.

Delphi
procedure TForm1.Delete1Click(Sender: TObject);
begin
  cxMCListBox1.DeleteSelected;
end;
procedure TForm1.cxMCListBox1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  ClickedItemIndex: Integer;
  PopupMenuOrigin: TPoint;
begin
  if Button <> mbRight then Exit;
  ClickedItemIndex := cxMCListBox1.ItemAtPos(Point(X, Y - 16), True);
  if ClickedItemIndex <> -1 then
  begin
    cxMCListBox1.ClearSelection;
    cxMCListBox1.Selected[ClickedItemIndex] := True;
    PopupMenuOrigin := cxMCListBox1.ClientToScreen(Point(X, Y));
    PopupMenu1.Popup(PopupMenuOrigin.X, PopupMenuOrigin.Y);
  end;
end;

The result of handling events in such a manner is illustrated in the image below.

See Also