Hola angelp4492.
Obtener el valor seleccionado mediante el click/doble click no es tan sencillo como en otros controles, por ejemplo un
TStringGrid:
Código Delphi
[-]
procedure TForm1.StringGrid1Click(Sender: TObject);
begin
Caption:= StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row];
end;
Ya que el evento
OnClick del
TListView se dispara ante cualquier click en el área cliente del control; pero dada la multiplicidad de estilos el click puede ser echo en una etiqueta, un ícono, un state icon,..,o en nada. Por otro lado tenemos que la propiedad
SubItems de
TListItem es de tipo
TStrings.
Con la función
GetHitTestInfoAt del
TListView podemos obtener información de donde fue hecho el click y de ese modo obtener los
SubItems del
TListItem (si el click fué hecho sobre alguno).
Código Delphi
[-]
function GetListViewItem(LV: TListView): TStrings;
var
HitTests: THitTests;
P: TPoint;
begin
P:= LV.ScreenToClient(Mouse.CursorPos);
HitTests:= LV.GetHitTestInfoAt(P.X,P.Y);
Result:= TStringList.Create;
if HitTests <= [htOnIcon, htOnItem, htOnLabel, htOnStateIcon] then
Result:= LV.Selected.SubItems
end;
Llamada de ejemplo:
Código Delphi
[-]
procedure TForm1.ListViewClick(Sender: TObject);
begin
ListBox1.Items:= GetListViewItem(ListView)
end;
Tu pregunta inicial fué sobre seleccionar un SubItem, pero lamentablemente no conozco la forma de conocer por ejemplo, sobre cuál
SubItem de un ListView con
ViewStyle =
vsReport fué hecho el click. Cuando se hace un click sobre un
SubItem el valor obtenido es
htNowhere, cosa fácil de comprobar agregando, este código al evento
OnClick del
TViewList:
Código Delphi
[-]
procedure TForm1.ListViewClick(Sender: TObject);
var
HitTests: THitTests;
P: TPoint;
begin
P:= ListView.ScreenToClient(Mouse.CursorPos);
HitTests:= ListView.GetHitTestInfoAt(P.X,P.Y);
with ListBox1.Items do
begin
if htAbove in HitTests then Add('htAbove');
if htBelow in HitTests then Add('htBelow');
if htNowhere in HitTests then Add('htNowere');
if htOnItem in HitTests then Add('htOnItem');
if htOnButton in HitTests then Add('htOnButton');
if htOnIcon in HitTests then Add('htOnIcon');
if htOnIndent in HitTests then Add('htOnIndent');
if htOnLabel in HitTests then Add('htOnLabel');
if htOnRight in HitTests then Add('htOnRight');
if htOnStateIcon in HitTests then Add('htOnStateIcon');
if htToLeft in HitTests then Add('htToLeft');
if htToRight in HitTests then Add('htToRight');
end;
end;
Espero te ayude en algo.
Saludos.