How to find the column and row of a basic tableField

Niggemann, Bernd Bernd.Niggemann at
Wed Apr 1 15:22:24 EDT 2020

There was a long thread regarding the dataGrid which somehow touched getting the row and column of a basic tableField.
I posted this solution also there. Mike Doub found some problems that turned out to be due to the fact that "cellEdit" was set to true for the table object.
If "cellEdit" is true then a front script takes over and creates a temporary field.

To post my solution to the problem again I start this new thread for clarity. The script is an elaboration on a sketchy script by Jacque.

The basic tableField is a regular field which has the option of "cellEditing". Apart from that it is a field that uses tab-delimited data to display it in a table form. 
hGrid and vGrid show horizontal and vertical dividers.

To get at the column number and the row number to determine the cell the user clicked on when the field's lockText is true here is a script that works well for me.

To test this make a regular field, name it "tf" and a second field for the result named "fRes". Make sure that "cellEdit" of the field is set to false (tab "table" in Properties Inspector)

set the script of field "tf" to 

on mouseUp
   put word 2 of the clickLine into tLine
   put getColumn(the clickH) into tItem
   put "Row:" && tLine into tRow
   put "Col:" && tItem into tColumn
   set the itemDelimiter to tab
   if tItem is not empty then
      put tRow &cr& tColumn & cr & "ClickH: " & the clickH & cr & "content: " & item tItem of line tLine of me & cr  into field "fRes"
      put "No Item" into tItem
      put tRow &cr& tColumn & cr & "ClickH: " & the clickH & cr & "content: " & tItem  into field "fRes"
   end if
end mouseUp

function getColumn pClickH
   put the tabstops of me into tTabs
   put the num of items in tTabs into tNumStops
   if tNumStops > 1 then
      put last item of tTabs - item -2 of tTabs into tTabWidth
      put item 1 of tTabs into tTabWidth
   end if
   set the itemdel to tab
   put the num of items in line 1 of me into tColumns
   if tNumStops < tColumns then -- add missing tabstops
      repeat with x = tNumStops+1 to tColumns
         put comma & (tTabWidth * x) after tTabs
      end repeat
   end if
   set the itemdel to comma
   put the hScroll of me into tHScroll
   put the borderwidth of me into tBrdr -- tested from 0 to 4
   put the leftMargin of me - 3 into tLeftMarg -- tested from 4 to 12
   put the left of me + tBrdr into tLeftAndBorder
   put tLeftAndBorder - tHScroll - tLeftMarg into tLeftOfItem
   put tLeftAndBorder - tHScroll + tLeftMarg + 1 into tRightOfItem
   put 0 & comma before tTabs
   repeat with x = 1 to the num of items in tTabs
      if pClickH > (item x of tTabs) + tLeftOfItem and \
            pClickH < (item x+1 of tTabs) + tRightOfItem then
         return x
      end if
   end repeat
   return empty
end getColumn

add a button to the card with the following script for test data

on mouseUp
   put "1,2,3,4,5,6" into tLine
   put empty into field "tf"
   replace comma with tab in tLine
   set the itemDelimiter to tab
   repeat 6
      put tLine & cr after tCollect
      repeat with i = 1 to 6
         add 6 to item i of tLine
      end repeat
   end repeat
   delete last char of tCollect
   lock screen
   put tCollect into field "tf"
   set the hGrid of field "tf" to true
   set the vGrid of field "tf" to true
   set the vScrollbar of field "tf" to true
   set the tabStops of field "tf" to 77
   set the lockText of field "tf" to true
   unlock screen
end mouseUp


Kind regards

More information about the use-livecode mailing list