DataGrid question...

David Epstein dfepstein at
Mon Mar 30 09:13:10 EDT 2020

This is more or less Jacqueline Gay’s suggestion, but with a couple of wrinkles.  You do need to adjust for the horizontal scroll of the field (I’m not sure about borders or margin).  And if I am not mistaken sometimes “the tabStops” will not have an entry for each tab, if the column width stays constant.
My approach is to keep a global called gColGuide, which gets reloaded when column widths change.  It has one line per column, with leftEdge,0,rightEdge,0 in each line.
The function used to load gColGuide is shown below.

put word 1 of the clickLoc into x
put word 2 of the clickLine into L — line number
put the hScroll of fld “table” into hs

put colGuide() into gColGuide

function mCol x,hs — returns column number for horizontal pixel x, consulting gColGuide.  hs is the field’s hScroll.
  global gColGuide
  add hs to x
  put 1 into c
  repeat for each line k in gColGuide
    if x > item 1 of k and x < item 3 of k then return c
    add 1 to c
  end repeat
  return empty
end mCol-- Any need to adjust for borders and margins?

function colGuide — used to load gColGuide with one line per column; each line has left,0,right,0
  set itemDelimiter to tab
  put the number of items in fld "headers" of into cz
  set itemDelimiter to comma
  get the tabStops of fld “table”
  put "0," before it
  put the left of fld “table” into fL
  repeat with n = 1 to min(cz,(-1 + the number of items in it)) -- so that colGuide will only cover the used columns.
    put fL+8 + item n of it,0,fL+8 + item n+1 of it,0 & return after hold
  end repeat
  if cz > n then
    put item 3 of line -1 of hold - item 1 of line -1 of hold into w
    repeat with m = n+1 to cz
      get item 3 of line -1 of hold
      put it,0,w + it,0 & return after hold
    end repeat
  end if
  put fL into item 1 of hold
  return hold
end colGuide

David Epstein

More information about the use-livecode mailing list