Recent

Author Topic: [SOLVED] Restrict a drag to ScrollBox bounds (drag-drop not working)  (Read 1353 times)

carlangas

  • Jr. Member
  • **
  • Posts: 57
I know it must be trivial, but I cannot manage to solve it.

I draw some custom drawn items (basically 200x200 px panels containing a TImage), in a ScrollBox so that they are a row. They are placed one above the other and I use the scroll bar to move among them.

The thing is I want to drag and drop them to swap their order. I connect an OnMouseDown and an OnMouseUp event to the items to control this. Everything works fine to know which of them is the one I press the button on, but I cannot know at which position I release the button. Well, the truth is I can guess it using the Y property of OnMouseUp as I know the height of every item, but it only gives me the relative distance in pixels to the starting point, and it might happen that I had released the button out of the ScrollBox so there's no item at that position. After calculating, it would give a wrong item number.

I have tried the OnMouseEnter and OnMouseLeave event of the ScrollBox, but they seem to stop working until the dragging is over.

Is there a simple way to know whether the mouse is within the Top and Bottom bounds of the ScrollBox?

Thanks.
« Last Edit: June 17, 2019, 10:04:58 am by carlangas »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Restrict a drag to ScrollBox bounds
« Reply #1 on: June 17, 2019, 12:47:27 am »
There is an event "OnDragOver" this is where you accept the dragging.
the OnDragDrop shouldn't get called If you reject it.

 If you are scrolling the box then include the scrollbar position in your calculations.
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Restrict a drag to ScrollBox bounds
« Reply #2 on: June 17, 2019, 12:54:56 am »
Rather than doing it all by hand, why don't you use drag'n'drop events? Using them makes it all much more easy. Search the help files for these:
  • OnStartDrag
  • OnDragOver
  • OnEndDrag
  • OnDragDrop

ETA: Yeah, what jamie said :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

carlangas

  • Jr. Member
  • **
  • Posts: 57
Re: Restrict a drag to ScrollBox bounds
« Reply #3 on: June 17, 2019, 09:50:30 am »
No way. I tried those events, but it seems it doesn't enter them, even if I disconnect the MouseUp and MouseDown events, nothing happens for OnStartDrag, OnDragOver, ...

I will have a look at the help files because I know there must be something that prevents them from working properly. Maybe I twisted it too much and forgot some minor detail.

carlangas

  • Jr. Member
  • **
  • Posts: 57
Re: Restrict a drag to ScrollBox bounds
« Reply #4 on: June 17, 2019, 10:03:38 am »
Thanks, guys, I found it. Somehow I overlooked this issue while browsing the documentation.

As expected, it was a silly thing. I had not changed the DragMode to dmAutomatic.

I have only tried that the OnDragOver event calls the corresponding procedure yet, but that's what I was looking for.

Thanks again.

 

TinyPortal © 2005-2018