Recent

Author Topic: listbox.clearselection if click on whitespace  (Read 3433 times)

bagofmoes

  • New Member
  • *
  • Posts: 11
listbox.clearselection if click on whitespace
« on: October 09, 2015, 02:08:06 pm »
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

  • Guest
Re: listbox.clearselection if click on whitespace
« Reply #1 on: October 09, 2015, 02:25:09 pm »
Hi  bagofmoes,

Try this:
Code: Pascal  [Select][+][-]
  1. var
  2.   ValidItem: Boolean;
  3.  
  4. procedure TForm1.BuildingBoxMouseDown(Sender: TObject; Button: TMouseButton;
  5.   Shift: TShiftState; X, Y: Integer);
  6. begin
  7.    ValidItem := BuildingBox.GetIndexAtXY(X, Y) <> -1;
  8. end;
  9.  
  10. procedure TForm1.BuildingBoxDblClick(Sender: TObject);
  11. begin
  12.   if ValidItem then
  13.     //show popup
  14. end;
  15.  

regards

bagofmoes

  • New Member
  • *
  • Posts: 11
Re: listbox.clearselection if click on whitespace
« Reply #2 on: October 09, 2015, 02:35:09 pm »
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

  • Guest
Re: listbox.clearselection if click on whitespace
« Reply #3 on: October 09, 2015, 02:38:47 pm »
What is your OS? It's working fine on windows.

bagofmoes

  • New Member
  • *
  • Posts: 11
Re: listbox.clearselection if click on whitespace
« Reply #4 on: October 09, 2015, 02:40:29 pm »
windows 10, 64bit, lazarus IDE v1.4.2

balazsszekely

  • Guest
Re: listbox.clearselection if click on whitespace
« Reply #5 on: October 09, 2015, 02:56:00 pm »
Quote
nope 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][+][-]
  1. procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  2.   Y: Integer);
  3. var
  4.   P: TPoint;
  5. begin
  6.   P.X := X;
  7.   P.Y := Y;
  8.   if Listbox1.ItemAtPos(P, true) <> -1 then
  9.      showmessage(inttostr(P.x) + ' ' + inttostr(P.y));
  10. end;
  11.  

bagofmoes

  • New Member
  • *
  • Posts: 11
Re: listbox.clearselection if click on whitespace
« Reply #6 on: October 09, 2015, 02:59:08 pm »
ow now I see! sorry. I see I misunderstood your code. I'll try it out!

bagofmoes

  • New Member
  • *
  • Posts: 11
Re: listbox.clearselection if click on whitespace
« Reply #7 on: October 09, 2015, 03:04:16 pm »
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

  • Guest
Re: listbox.clearselection if click on whitespace
« Reply #8 on: October 09, 2015, 03:06:41 pm »
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][+][-]
  1. var
  2.   P: TPoint;
  3. begin
  4.   P := Mouse.CursorPos;
  5.   P := ScreenToClient(P);
  6.   P.X := P.X - ListBox1.Left;
  7.   P.Y := P.Y - ListBox1.Top;
  8.   if Listbox1.ItemAtPos(P, True) <> -1 then
  9.     ShowMessage(IntToStr(P.X) + ' ' + IntToStr(P.Y));
  10.  

bagofmoes

  • New Member
  • *
  • Posts: 11
Re: listbox.clearselection if click on whitespace
« Reply #9 on: October 09, 2015, 03:13:32 pm »
If you wouldn't have answered, I would have probably worked towards that haha

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: listbox.clearselection if click on whitespace
« Reply #10 on: October 09, 2015, 05:21:04 pm »
Code: Delphi  [Select][+][-]
  1. var
  2.   P: TPoint;
  3. begin
  4.   P := Mouse.CursorPos;
  5.   P := ListBox1.ScreenToClient(P);
  6.   if Listbox1.ItemAtPos(P, True) <> -1 then
  7.     ShowMessage(IntToStr(P.X) + ' ' + IntToStr(P.Y));
  8.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

balazsszekely

  • Guest
Re: listbox.clearselection if click on whitespace
« Reply #11 on: October 09, 2015, 05:43:40 pm »
@taazz
Good catch!

 

TinyPortal © 2005-2018