LiveCode LessonsData GridLiveCode Data GridWorking With Data Grids (Forms & Tables)How Do I Add a mouseDown Event To The Data Grid Without Breaking It?

How Do I Add a mouseDown Event To The Data Grid Without Breaking It?

This lesson will show you how you to write your own mouseDown event in a Data Grid without breaking the default Data Grid behavior. You need to know how to do this when showing a contextual menu or if during the mouseDown event you want to use the data in the row the user clicked on.

When you add a mouseDown handler to a Data Grid you are intercepting a message that the Data Grid normally handles. Doing so changes the behavior of the Data Grid and you need to take that into account when coding your mouseDown handler.

What Doesn't Work

If you were to place the following code in your Data Grid script you would not get the result you were expecting.  The reason is that the line that was clicked on would not be selected until after your popup menu was displayed. Why? Because the Data Grid behavior script processes mouseDown AFTER the mouseDown handler you define in the Data Grid script itself.

on mouseDown pMouseBtnNum
   if pMouseBtnNum is 3 then
      ## Oops! Line that user clicked on has not been selected since 
      ## Data Grid has not processed mouseDown yet. Contextual
      ## menu won't target proper line.
      popup button "MyContextualMenu"
   end if
   pass mouseDown
end mouseDown

What Does Work: dgMouseDown

In order to work around this the Data Grid wraps all mouseDown functionality in a handler named dgMouseDown. You can call this handler in your code in order to get the expected results.

The following examples show how you can define a mouseDown handler in a Data Grid script.  In the examples the line the user clicks on will be selected before the custom mouseDown code executes. Note that I don't pass mouseDown after calling dgMouseDown. This would only repeat the call to dgMouseDown which I don't want to do.

###########
# Example: Contextual Menu
###########
on mouseDown pMouseBtnNum
   ## Let Data Grid process mouseDown and select row that was clicked on
   dgMouseDown pMouseBtnNum		
   		
   ## Now contextual menu will act on the proper line.
   if pMouseBtnNum is 3 then
      popup button "MyContextualMenu"
   end if
   	
   ## Don't pass mouseDown
end mouseDown
###########
# Example: Getting Value From Cell Clicked On (Data Grid table)
###########
on mouseDown pMouseBtnNum
   local tColumnValue

   ## Let Data Grid process mouseDown and select row that was clicked on
   dgMouseDown pMouseBtnNum		
   		
   ## Get value of column clicked on. The column name can be accessed in the
   ## dgColumn custom property of the column control (the target).
   put GetDataOfIndex(the dgHilitedIndexes of me, the dgColumn of the target) into tColumnValue
   	
   ## Don't pass mouseDown
end mouseDown

2 Comments

kee nethery

dgMouseDown is not in the Dictionary. What else in the dgMessageLineage is not in the Dictionary? Is there a dgMouseStillDown lurking somewhere? That's what I'm looking for right now.

Elanor Buchanan

Hi Kee

The dgMouseDown message is not in the Dictionary because it is mainly internal to the DataGrid code. I don't believe there is a dgMouseStillDown anywhere.

If you could report this as a documentation bug that would be very helpful, we can then decide the best way to document these messages.

Kind regards

Elanor

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.