PDA

Ver la Versión Completa : Selección de Subitem de ListView


angelp4492
17-04-2012, 12:50:24
Hola estoy trabajando con el control ListView y ya me tiene loco, quiero selecciónar un Subitem al hacer doble click sobre el elemento, alguna solución . Gracias de antemano.

ecfisa
18-04-2012, 04:55:20
Hola angelp4492.

Obtener el valor seleccionado mediante el click/doble click no es tan sencillo como en otros controles, por ejemplo un TStringGrid:

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).

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:

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:

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.

angelp4492
18-04-2012, 13:29:06
Gracias despues de ver el lio que conlleva este tema, he cambiado por un control TTreeview y listo cosa fácil.

roman
18-04-2012, 18:44:05
Hay una forma de obtener el subitem sobre el que se da clic:


uses CommCtrl;

procedure TForm1.ListViewMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Item: TListItem;
Info: TLVHitTestInfo;
Index: Integer;

begin
Info.pt := Point(X, Y);
Index := ListView_SubItemHitTest(ListView.Handle, @Info);

if Index <> -1 then
begin
Item := ListView.Items[Index];

if Info.iSubItem = 0
then ShowMessage(Item.Caption)
else ShowMessage(Item.SubItems[Info.iSubItem - 1]);
end
else
ShowMessage('nowhere');
end;


// Saludos

ecfisa
18-04-2012, 18:55:24
Hola roman.

No puedo encontrar la clase TLVHitTestInfo ni la función ListView_SubItemHitTest en Delphi 7, ¿ Vienen en una versión posterior ?

Saludos.:)

roman
18-04-2012, 19:06:44
Vienen en Delphi 7, en la unidad CommCtrl (no confundir con la unidad ComCtrls que es donde está el ListView).

// Saludos

ecfisa
18-04-2012, 19:19:49
Vienen en Delphi 7, en la unidad CommCtrl (no confundir con la unidad ComCtrls que es donde está el ListView).

// Saludos
Muchas gracias por la información, suponía que tenía que existir modo de obtenerlo. Pero no es un control que use mucho y realmente no encontré ese dato...

Saludos.

maeyanes
18-04-2012, 19:32:59
Hola...

Por favor eficsa, no dupliques tus mensajes...

:D :D :D


Saludos...

ecfisa
18-04-2012, 19:37:53
Hola...

Por favor eficsa, no dupliques tus mensajes...

Saludos...
Hola maeyanes.

Mis sinceras disculpas :o, ya mismo paso a pegarle una leída a la guía de estilo :)

Saludos. :)

Edito: Borré el segundo mensaje, por que aunque estaba muuuuy bonito, no tanto como para leerlo dos veces :D:D:D