[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 603
  • Last Modified:

Mouse over items in TListview

I have a listview containing some items. When I move the mouse over a specific item I want
some things to happen. I see that TListView has a MouseMove event that I can use, but I'm not
sure about the code. I want it to be something like this (pseudo-code):
 
procedure TMainForm.ListView1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
var mOverItem: integer;
 
//code for finding out which item the mouse is over. Stored in mOverItem. -1 if none.
 
//calls a procedure with the mOverItem as a parameter. don't worry about this one. I can handle this myself.
 
end;
 
is this possible?
the listview can have different number of items.

cheers,
mha
0
michaenh
Asked:
michaenh
  • 2
  • 2
1 Solution
 
kretzschmarCommented:
maybe ?

procedure TForm1.ListView1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var mOverItem: TListItem;
begin
  mOverItem := ListView1.GetItemAt(x,y);
  if mOverItem.Index = ....
  //or
  if mOverItem.Caption = ....

end;


meikl ;-)
0
 
michaenhAuthor Commented:
I did something like this:
 
procedure TMainForm.ListView2MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
mOverItem: TListItem;
begin
try
mOverItem := ListView1.GetItemAt(x,y);
label1.Caption := IntToStr(mOverItem.Index);
except on e: exception do
 
end;
end;

but my label only shows '0' when I hold my mouse over the first item. It doesn't react when i'm holding over the other items.
if i put a showmessage(e.message) in my except block i just get an access violation error. but it's then mOverItem.index that generates the exception I believe.
btw, my items have just image. their caption is blank.

thanks
0
 
kretzschmarCommented:
tested:

procedure TForm1.ListView1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  mOverItem: TListItem;
begin
  mOverItem := ListView1.GetItemAt(x,y);
  if assigned(mOverItem) then  //<--- you must do a test
    label1.Caption := inttostr(mOverItem.Index);


end;

meikl ;-)
0
 
michaenhAuthor Commented:
It worked!
My apologies but, I didn't see that I tried to get the ListView1.index in the ListView2 mousemove :=)

procedure TMainForm.ListView1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
mOverItem: TListItem;
begin
try
mOverItem := ListView1.GetItemAt(x,y);
SkrivForklaring(mOverItem.Index, ListView1);
except on e: exception do
SkrivForklaring(-1, ListView1);
end;
end;

thanks for your quick response! :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now