listbox.clearselection if click on whitespace lazarus / 31-LCL ---------------------------------------------------------------------- bagofmoes · 2015-10-09 12:08 hello, i've been struggeling with this for quite some time now. I'm writing this program with a listbox. I retrieve items from an sql database and everything works well. however, i want to implement some options for the items. therefor I use a popup menu in a doubleclickevent. but the pop up also apears when clicking on whitespace. I bet there must be a simple solution to my problem. I think I can best show my problem with a test: procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,   Y: Integer); begin   if BuildingBox.ItemAtPos(mouse.CursorPos,true) <> -1 then   showmessage(inttostr(mouse.CursorPos.x) + ' ' + inttostr(mouse.CursorPos.y)); end; so here is a procedure which will trigger a showmessage as soon as the mouse would hover over a listbox item... however... this doesn't happen. Why do these coordinates not match? I wanted to figure it out by myself, but I can't seem to find a function where the position of an item is returned giving the index. I hope you have an answer to my problem! thanks in advance ---------------------------------------------------------------------- balazsszekely · 2015-10-09 12:25 Hi  bagofmoes, Try this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  ValidItem: Boolean; procedure TForm1.BuildingBoxMouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin   ValidItem := BuildingBox.GetIndexAtXY(X, Y) <> -1;end; procedure TForm1.BuildingBoxDblClick(Sender: TObject);begin  if ValidItem then    //show popupend;  regards ---------------------------------------------------------------------- bagofmoes · 2015-10-09 12:35 nope sorry, same result. it's basicly the same principle as my test function. tested it just to be sure, but no succes. i'm thinking the cursorposition doesn't have the same x, y coordinates as the listboxitem. like the item of the listbox has coordinates based on the form height and width and the cursor has the whole screen. this would result that it would never match.. ---------------------------------------------------------------------- balazsszekely · 2015-10-09 12:38 What is your OS? It's working fine on windows. ---------------------------------------------------------------------- bagofmoes · 2015-10-09 12:40 windows 10, 64bit, lazarus IDE v1.4.2 ---------------------------------------------------------------------- balazsszekely · 2015-10-09 12:56 Quotenope sorry, same result. it's basicly the same principle as my test function. tested it just to be sure, but no succes.No it's not. I just noticed,  you check on FormMouseMove not ListBoxMouseMove and you don't need Mouse.CursorPos since you already have the x, y coordinates. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);var  P: TPoint;begin  P.X := X;  P.Y := Y;  if Listbox1.ItemAtPos(P, true) <> -1 then     showmessage(inttostr(P.x) + ' ' + inttostr(P.y));end; ---------------------------------------------------------------------- bagofmoes · 2015-10-09 12:59 ow now I see! sorry. I see I misunderstood your code. I'll try it out! ---------------------------------------------------------------------- bagofmoes · 2015-10-09 13:04 YES it works! before I tried this I did a for loop for every pixel on the screen, see where the function wouldn't result -1. this was on position 1,1 to 1,xx so it makes perfectly sense now:D. thank you ---------------------------------------------------------------------- balazsszekely · 2015-10-09 13:06 Ok, I'm glad it's working. If your really want to use Mouse.CursorPos(although in this case is not justified): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  P: TPoint;begin  P := Mouse.CursorPos;  P := ScreenToClient(P);  P.X := P.X - ListBox1.Left;  P.Y := P.Y - ListBox1.Top;  if Listbox1.ItemAtPos(P, True) <> -1 then    ShowMessage(IntToStr(P.X) + ' ' + IntToStr(P.Y)); ---------------------------------------------------------------------- bagofmoes · 2015-10-09 13:13 If you wouldn't have answered, I would have probably worked towards that haha ---------------------------------------------------------------------- taazz · 2015-10-09 15:21 Code: Delphi  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  P: TPoint;begin  P := Mouse.CursorPos;  P := ListBox1.ScreenToClient(P);  if Listbox1.ItemAtPos(P, True) <> -1 then    ShowMessage(IntToStr(P.X) + ' ' + IntToStr(P.Y)); ---------------------------------------------------------------------- balazsszekely · 2015-10-09 15:43 @taazz Good catch!