Setting focus to a Data Grid

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

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Setting focus to a Data Grid

Post by fritzdekatt » Thu Oct 24, 2019 12:12 am

I can't imagine this hasn't been brought up, but I did a search here and didn't see an exact match (though a Google search mentions the topic a few times).
How do you set the focus to a datagrid? I have tried this below with a test button:

on mouseUp pButtonNumber
set the dgfocus of group "List" to true
--focus on group "List"
end mouseUp


I should mention I'm on a Mac, and I know the focus works differently on Mac than WIndows. MacOS Sierra on MacBook Pro.

The focus command didn't work so somebody online mentioned the "dgfocus". Neither of these worked. Somebody asked if the "set traversal" was true, but I don't know how or where I'd set this. I figured there must be a property setting for this but I don't see it. Tabbing through my screen doesn't seem to affect the datagrid. So, if someone could tell me how to set the traversal, that would be nice. I tried to set focus to a column inside the grid but that also didn't work. If there's a focus command I haven't tried, please set me straight.
Thank you
fritz

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

Re: Setting focus to a Data Grid

Post by dunbarx » Thu Oct 24, 2019 2:34 am

Hi.

This is an odd question to me.

The dictionary says:
Places the insertion point, makes a control active or removes focus from all controls
A dataGrid is a group of controls, behaviors and who knows what else. The entire concept of "focus" has always been relevant, to me at least, only to a single control. You are not talking about "selecting" a dataGrid, correct?

If one were able to focus on a DG, what would, visually or functionally, change? More simply, if you had a group of two buttons, say, what would focusing on that group look like?

Craig

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Setting focus to a Data Grid

Post by fritzdekatt » Thu Oct 24, 2019 10:06 am

I want to be able to use my tab key (and/or code activated by a button or another control) to "enter" the datagrid and "enter" the first field therein for navigating and editing purposes. I can already navigate and edit via mouse but I'd love to use the keyboard to go from one control to another on my screen, including the datagrid, to edit records. I don't want to have to click on it to use the control. By "use" I primarily mean "edit". The contents are linked to a table in a MySQL database, if that makes any difference.

This morning I got the tab key and my test "focus" button to highlight the first line of the datagrid, but I still can't "enter" the field instance of the column ("edit" the field by selecting it programmatically). The arrow keys work for navigation, however. I don't know why it "suddenly" started obeying the tab control and test button, but I guess I might have changed the highlight settings in the properties inspector to make the focus obvious to my aging eyes.

To summarize, my question would be: how do I set the focus to a control (field) inside a datagrid for editing purposes via code? It already works fine when I double-click.

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

Re: Setting focus to a Data Grid

Post by dunbarx » Thu Oct 24, 2019 2:57 pm

Hmmm.

I tried:

Code: Select all

click at the loc of fld "col 1 0001"
but that did not work, nor did clicking twice. That sort of kluge could be managed. Or you could wait for a reply from someone who actually know something about dataGrids.

Craig

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Setting focus to a Data Grid

Post by fritzdekatt » Thu Oct 24, 2019 9:53 pm

Craig:
How bout if I try both? I'll try a variant (or two) of your idea while waiting for whoever cares to comment further. I get to learn something either way.
Thanks for replying.

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

Re: Setting focus to a Data Grid

Post by MichaelBluejay » Thu Oct 24, 2019 10:00 pm

fritzdekatt, Data Grid has its own syntax that's separate from normal LiveCode syntax. Unfortunately it's poorly documented. While I don't know the exact answer to your question, I'm sure you'll be using the EditCell or EditFieldText command (either in the DG script, or sent to the DG), somehow. When I tried send "EditCell theColumnName, theLineNumber" to my grid, the cell blinked for a second but didn't remain open for editing. So that seems to be on the right track, maybe you can take that and figure out how to get to the finish line. It would be nice if this kind of thing were documented.

This selects a line but does not open a cell:

Code: Select all

click at 834,58
wait 1 milliseconds
click at 834,58
Setting the delay higher doesn't help.
Last edited by MichaelBluejay on Thu Oct 24, 2019 11:59 pm, edited 2 times in total.

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Setting focus to a Data Grid

Post by fritzdekatt » Thu Oct 24, 2019 10:26 pm

A little flicker of a response is enough to go on at the moment. I'll play with it a bit.
Thank you, Michael.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Setting focus to a Data Grid

Post by SparkOut » Fri Nov 01, 2019 3:31 pm

Did you get anywhere with this?
This should work to open the "phantom" edit field.

Code: Select all

dispatch "EditFieldText" to group "MyDataGrid" with (tLongId)
where tLongId is the long ID of the field you want to be automatically opened for editing. Unfortunately the DataGrid does not appear to be set up for keyboard navigation with tab and arrowkeys though.

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

Re: Setting focus to a Data Grid

Post by MichaelBluejay » Wed Nov 06, 2019 2:09 pm

Thank you, SparkOut, this works. Some notes:

(1) It does *not* work from the message box! So trying it in the message box could lead you to believe that the code is bad, when the code could be good. Probably the focus is being returned to the message box after executing, so you don't see the cell remain open.

(2) You get the id of the field by identifying the column and the row. So, for a cell in the column called "Date", first row:

Code: Select all

   put the long id of field "Date 0001" into fieldID
   dispatch "EditFieldText" to group "myDataGrid" with fieldID

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

Re: Setting focus to a Data Grid

Post by dunbarx » Wed Nov 06, 2019 3:39 pm

Michael.

There is a current thread on just this topic, that the message box is not reliable invoking commands.
viewtopic.php?f=9&t=33282

I have learned to quickly make a button, run my (usually) one-liner, and then discard the button.

Craig

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Setting focus to a Data Grid

Post by fritzdekatt » Wed Nov 06, 2019 7:45 pm

SparkOut wrote:
Fri Nov 01, 2019 3:31 pm
Did you get anywhere with this?
This should work to open the "phantom" edit field.

Code: Select all

dispatch "EditFieldText" to group "MyDataGrid" with (tLongId)
where tLongId is the long ID of the field you want to be automatically opened for editing. Unfortunately the DataGrid does not appear to be set up for keyboard navigation with tab and arrowkeys though.
I could not select a Data Grid column or field with this code, though MichaelBluejay says he has had some luck. Assuming he was using a column in a data grid, I'm assuming I'm doing something wrong. I did get this code to reference a field on my form very readily. Just not in a data grid. I did try to select the grid with dgfocus beforehand but it still reported I was selecting another field outside the data grid. My problem may be from my inability to use the correct syntax for selecting a field (or column) inside a data grid.

Thanks
fritz

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

Re: Setting focus to a Data Grid

Post by MichaelBluejay » Tue Nov 12, 2019 10:37 am

Good process for troubleshooting is to get a simple version that works, then keep adding complexity until it breaks. For example:

(a) Create a new stack.
(b) Drag a new Data Grid out of the tool palette.
(c) In the Property Inspector > Content, add a word of data.
(d) Create a button with this script:

Code: Select all

on mouseup
  put the long id of field "Col 1 0001" into fieldID
  dispatch "EditFieldText" to group "DataGrid 1" with fieldID
end mouseup
Should work. If so, keep adding project details to this grid, checking the focus ability along the way, until it breaks.

If even the simple example didn't work, then your IDE is screwed up somehow.

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Setting focus to a Data Grid

Post by fritzdekatt » Tue Nov 12, 2019 11:55 am

MichaelBluejay wrote:
Tue Nov 12, 2019 10:37 am
Good process for troubleshooting is to get a simple version that works, then keep adding complexity until it breaks. For example:

(a) Create a new stack.
(b) Drag a new Data Grid out of the tool palette.
(c) In the Property Inspector > Content, add a word of data.
(d) Create a button with this script:

Code: Select all

on mouseup
  put the long id of field "Col 1 0001" into fieldID
  dispatch "EditFieldText" to group "DataGrid 1" with fieldID
end mouseup
Should work. If so, keep adding project details to this grid, checking the focus ability along the way, until it breaks.

If even the simple example didn't work, then your IDE is screwed up somehow.
Michael

This is the problem I've been encountering all along, and it's probably because I don't understand how to isolate a field inside the data grid.
LC can't seem to find "Col 1 0001", or a name of my own choosing for a column, to put into fieldID.

First of all, can you tell me where you got "Col 1 0001"?

Secondly, can I just name a column inside the data grid and use that in its place?

Thirdly, can I reference a field/column inside a data grid without selecting or referencing the data grid? I'm a noob but I certainly have no trouble getting data out of any other field. I've gotten data out of data grids too when I write from a select query into editable fields (Which is the process I'm trying to circumvent with learning to edit the data grid).

Anyway, I did what you said and started from scratch, which is always a good premise. If you could let me know where this column reference comes from I will jump on it.

Thank you
fritz

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

Re: Setting focus to a Data Grid

Post by MichaelBluejay » Tue Nov 12, 2019 2:29 pm

fritzdekatt wrote:LC can't seem to find "Col 1 0001", or a name of my own choosing for a column, to put into fieldID.
What do you mean "LC can't seem to find" the column? Are you getting an error when you're using my code, following my instructions in the previous post? If so, what is the error?
fritzdekatt wrote:First of all, can you tell me where you got "Col 1 0001"?
"Col 1" is the default name of the first column. "0001" is the first row. So, with "Col 1 0001" we're targeting the first row of the first column.

Incidentally, note that "0001" refers to the first *visible* row. If you've got 100 rows but only rows 51-100 are visible because you're scrolled all the way to the bottom, then "0001" refers to the first visible row, which is row 51 of the grid.
fritzdekatt wrote:Secondly, can I just name a column inside the data grid and use that in its place?
Yes, but only after you've tried my instructions and code in my previous post to make sure that that works.
fritzdekatt wrote:Thirdly, can I reference a field/column inside a data grid without selecting or referencing the data grid? I'm a noob but I certainly have no trouble getting data out of any other field.
[Yes, see dunbarx's reply below.]
fritzdekatt wrote:Anyway, I did what you said and started from scratch, which is always a good premise.
(1) Starting from scratch WITH MY INSTRUCTIONS/CODE? And if so,

(2) DID IT WORK?!
Last edited by MichaelBluejay on Tue Nov 12, 2019 6:04 pm, edited 1 time in total.

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

Re: Setting focus to a Data Grid

Post by dunbarx » Tue Nov 12, 2019 3:10 pm

Important to know that we are talking about FIELD "col 1 0001".

This is how a DG orders its fields. Note that this process is dynamic, in that one must create a suite of fields in order for the DG to create and name them. So once in hand, you can:

Code: Select all

select fld "col 1 0001"
or

Code: Select all

put "fff" into fld "col 1 0001"
Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”