# Listbox drag and mouse event handler interactions

**URL:** <https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177>\
**Category:** General Programming\
**Created:** [9 March 2020 12:27 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177 "2020-03-09T12:27:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clothears](https://ifnotnil.com/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@Clothears](https://ifnotnil.com/u/Clothears)\
**Post date:** [9 March 2020 12:27 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/1 "2020-03-09T12:27:20Z")

</div>

I’ve been using the DragRow, DragOver, and DragReorderRows events to allow the user to reorganise the rows in their listbox. This works without problems. At the same time, I use the CellClick event to allow the user to effect another, separate, action when they click a row. The only problem here is that clicking and holding the mouse to effect the drag is also caught as a CellClick action, so the two actions interfere. In addition, the separate non-drag action also happens immediately the mouse is pressed. I would actually like the separate action to occur when the mouse is released, as that would allow the user to change their mind.

So, I thought I’d use MouseDown and MouseUp instead of CellClick, but adding a MouseDown handler to the mix kills all the drag actions. They no longer work, just as if the Drag handlers were not implemented at all.

How can I stop the MouseDown event handler from interfering with the drag logic?

I’m using 2019r3.1. I posted on the forum but don’t expect to get any replies there.

---

<div class="post-metadata">

**Author:** ![beatrixwillius](https://ifnotnil.com/user_avatar/ifnotnil.com/beatrixwillius/32/24_2.png) [@beatrixwillius](https://ifnotnil.com/u/beatrixwillius)\
**Post date:** [9 March 2020 17:25 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/2 "2020-03-09T17:25:37Z")

</div>

Events when doing selection and dragging can get confusing.

How about you do a context menu instead of using a CellClick?

---

<div class="post-metadata">

**Author:** ![Clothears](https://ifnotnil.com/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@Clothears](https://ifnotnil.com/u/Clothears)\
**Post date:** [9 March 2020 18:10 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/3 "2020-03-09T18:10:13Z")

</div>

Beatrix,

Interesting idea - thanks. I already have a context menu, so it would be easy enough to add another entry there. Hah - I bet now I’ll find that if I remove the CellClick handler, the drag stuff will not work. 🤣

I’ll give it a try. I must say it would be nice if Xojo were to document these event handler interactions.

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [9 March 2020 20:24 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/4 "2020-03-09T20:24:25Z")

</div>

Not tried but:

Add a boolean property clicked  
In MouseDown set it to true  
In MouseDrag set it to false  
In MouseUp do

```
If clicked then 
  doTheClickedAction
  clicked = false
End if
```

---

<div class="post-metadata">

**Author:** ![Clothears](https://ifnotnil.com/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@Clothears](https://ifnotnil.com/u/Clothears)\
**Post date:** [9 March 2020 20:53 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/5 "2020-03-09T20:53:04Z")

</div>

The fundamental problem is that as soon as I added the MouseDown handler, the Drag events ceased to fire at all. If they fired, then adding some interlock logic would probably be straightforward. But they don’t.

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [9 March 2020 21:13 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/6 "2020-03-09T21:13:01Z")

</div>

What are you returning from MouseDown? (Not at my Mac)

---

<div class="post-metadata">

**Author:** ![jmadren](https://ifnotnil.com/user_avatar/ifnotnil.com/jmadren/32/65_2.png) [@jmadren](https://ifnotnil.com/u/jmadren)\
**Post date:** [9 March 2020 21:16 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/7 "2020-03-09T21:16:52Z")

</div>

What Markus is getting at is this in the docs on MouseDown:

> [Return](https://docs.xojo.com/Return) [True](https://docs.xojo.com/True) if you are going to handle the MouseDown. In such a case:
> 
> - The Action event, if any, will not execute and the state of the object will not change.
> - You will receive the [MouseDrag](https://docs.xojo.com/MouseDrag_event) and [MouseUp events](https://docs.xojo.com/MouseUp_event).
> 
> If you return False, the system handles the MouseDown so the above event handlers do not get called.

---

<div class="post-metadata">

**Author:** ![Clothears](https://ifnotnil.com/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@Clothears](https://ifnotnil.com/u/Clothears)\
**Post date:** [9 March 2020 22:00 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/8 "2020-03-09T22:00:53Z")

</div>

Yeah, I know. I read that too. I’ve tried returning True and False in MouseDown, makes no difference. The Drag handlers are simply not called (to check, I put breakpoints at the start of each).

---

<div class="post-metadata">

**Author:** ![MarkusWinter](https://ifnotnil.com/user_avatar/ifnotnil.com/markuswinter/32/32_2.png) [@MarkusWinter](https://ifnotnil.com/u/MarkusWinter)\
**Post date:** [9 March 2020 23:18 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/9 "2020-03-09T23:18:30Z")

</div>

MouseDrag might fire, but DragReorderRows does not (and the drag is blocked)

So returning True in MouseDown means you have to deal with EVERYTHING afterwards yourself: selecting the row, dragging, dropping, reordering.

Can you use the DoubleClick event instead? No problem there.

---

<div class="post-metadata">

**Author:** ![Clothears](https://ifnotnil.com/letter_avatar_proxy/v4/letter/c/90db22/32.png) [@Clothears](https://ifnotnil.com/u/Clothears)\
**Post date:** [10 March 2020 10:19 UTC](https://ifnotnil.com/t/listbox-drag-and-mouse-event-handler-interactions/177/10 "2020-03-10T10:19:44Z")

</div>

OK, just tested again, perhaps more carefully. When testing with MouseDown yesterday it seemed that any return value would prevent the Drag actions, but today I see it’s only when returning True. Unfortunately I need to return True in order to have the MouseUp event fire. Double-click I use for something else already.

So, it’s looking more like I’ll implement Beatrix’ suggestion and add the single-click action to the context menu.
