Page 1 of 2

Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 6:09 pm
by BigameOver
Hi,
I'm creating an app with a table. I need to create an option for clicking on the table's cells. couldn't find a way to do it instead of using al lot of buttons... Any idea?
thanks :D

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 6:17 pm
by dunbarx
Hi.

When you say "click on a table's cells", what do you mean? Are you talking about a table field, so you can edit the "cells" within?

Or are you creating your own table with a group of controls, perhaps buttons or fields?

In any case, once you write back, we can fix this in about a minute.

Craig

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 6:23 pm
by BigameOver
Hi, thank you for your answer!
I am using table field and I don't want to edit the cells, I want to be able to "mouseDown" on each cell.

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 7:29 pm
by FourthWorld
See the field script in the attached example.

Code: Select all

on mouseDown
  -- Get clicked line:
  put word 2 of the clickLine into tLine
  -- Get clicked column:
  put the clickH into tH
  put 0 into tCell
  repeat for each item tWidth in the tabstops of me
    if tH < tWidth then exit repeat
    add 1 to tCell
  end repeat
  -- Select the clicked text:
  set the itemdel to tab
  select item tCell of line tLine of me
  -- Report details if needed:
  put "Line="& tLine && "Item="& tCell && \
        "Text="& the text of item tCell of line tLine of me \
        into fld "Display"
end mouseDown
Note that in this script, being able to determine the cell within a given line (which will likely extend further than the text within the cell) requires that all tabstops be explicitly set. See the Dictionary entry for "tabstops" for a description of that field property.

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 7:52 pm
by dunbarx
OK.

So do you want the user to click somewhere, have a dialog come up, enter some data, and have that data fill the "cell" where the click was?

If so, table fields need to be hacked just a bit to do this. Table fields are tab and return delimited. Know that you can:

Code: Select all

set the itemDel to tab
put "foo" into item 3 of line 2 of fld "yourtableField"
You could, in the field script(pseudo):

Code: Select all

on mouseDown
   put the topLeft of me into XY
   put the clickLoc into tLoc
   ask "What?"
   put it into temp
   set the itemDel to tab
   put temp into the item that corresponds to the distance from XY to tLoc
end mouseDown
That last line requires you to know the tabStops and the text height. Just some simple math. Can you do it?

Craig

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 8:01 pm
by FourthWorld
Are my posts not visible here?

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 8:26 pm
by BigameOver
Thank you so much guys! Its working!! :D :D

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 8:32 pm
by BigameOver
FourthWorld wrote:
Thu Jan 23, 2020 8:01 pm
Are my posts not visible here?
No, you helped me a lot!

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 8:34 pm
by bogs
I think it is just a case of timing, Richard.

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 8:44 pm
by FourthWorld
BigameOver wrote:
Thu Jan 23, 2020 8:32 pm
FourthWorld wrote:
Thu Jan 23, 2020 8:01 pm
Are my posts not visible here?
No, you helped me a lot!
Thanks, BigGameOver. I've lost count of the number of times I've posted a solution here and the conversation keeps going like no one saw it. Glad this one helped.

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 8:49 pm
by dunbarx
Are my posts not visible here?
Is this like asking "Are you asleep?"

Craig

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 9:18 pm
by dunbarx
Hi again.

This works, if you do not already have it the way you want it. Make a table field and set its tabStops to "75,150,225,300,375,450,525,600"

Put this into the field script:

Code: Select all

on mouseDown
   put the textheight of me into tHeight
   put item 1 of the  clickLoc - item 1 of left of me into x
   put item 2 of the  clickLoc - item 1 of top of me into y
   
   put y div theight into lineNum
   if y mod tHeight <> 0 then add 1 to lineNum
   
   repeat with z = 1 to the number of items of the tabStops of me
      if item z of the tabstops of me > x then
         put z into itemNum
         exit repeat
      end if
   end repeat
   ask "What?"
   set the itemDel to tab
   put it into item itemNum of line lineNum of me
end mouseDown
The only issue here is that a fresh table field has its tabStops set to "75". That is because LC interprets that single value as the same as "75,150,225,300..." So unless you manually set your own unique tabStops when you configure your field, you have to expand the default one. Otherwise the repeat loop will fail. Do you know what I mean?

Craig

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 9:19 pm
by FourthWorld
dunbarx wrote:
Thu Jan 23, 2020 8:49 pm
Are my posts not visible here?
Is this like asking "Are you asleep?"
Not quite. Subsequent posts seem unlikely to have been written while asleep. :)

Re: Accesing text of a cell in table field by clicking on it

Posted: Thu Jan 23, 2020 9:51 pm
by dunbarx
@Richard.

I do my best work asleep.

@BigameOver

We can avoid the tabStop issue by simply analyzing the current setting. If a single value, expand to infinity as I mentioned above. If explicit, it may be that the last value is not great enough. This is because LC will take the width of the last tab and extend that to infinity. So shall we.

This can be added as a small routine to the field itself, so that if you change its form, the handler will still run just fine.

Craig

Re: Accesing text of a cell in table field by clicking on it

Posted: Fri Jan 24, 2020 6:53 am
by BigameOver
@dunbarx
thanks!
I set the tabStops manually and its working fine :D