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

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

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

Post by BigameOver » Thu Jan 23, 2020 6:09 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu Jan 23, 2020 6:17 pm

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

BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

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

Post by BigameOver » Thu Jan 23, 2020 6:23 pm

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

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

Post by FourthWorld » Thu Jan 23, 2020 7:29 pm

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.
Attachments
ClickTable Example.zip
(912 Bytes) Downloaded 175 times
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu Jan 23, 2020 7:52 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

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

Post by FourthWorld » Thu Jan 23, 2020 8:01 pm

Are my posts not visible here?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

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

Post by BigameOver » Thu Jan 23, 2020 8:26 pm

Thank you so much guys! Its working!! :D :D

BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

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

Post by BigameOver » 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!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

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

Post by bogs » Thu Jan 23, 2020 8:34 pm

I think it is just a case of timing, Richard.
Image

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

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

Post by FourthWorld » Thu Jan 23, 2020 8:44 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu Jan 23, 2020 8:49 pm

Are my posts not visible here?
Is this like asking "Are you asleep?"

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu Jan 23, 2020 9:18 pm

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
Last edited by dunbarx on Thu Jan 23, 2020 9:23 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

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

Post by FourthWorld » Thu Jan 23, 2020 9:19 pm

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. :)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Thu Jan 23, 2020 9:51 pm

@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

BigameOver
Posts: 39
Joined: Thu Jan 23, 2020 5:56 pm

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

Post by BigameOver » Fri Jan 24, 2020 6:53 am

@dunbarx
thanks!
I set the tabStops manually and its working fine :D

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”