How to programmatically select a datagrid

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

Post Reply
CAsba
Posts: 423
Joined: Fri Sep 30, 2022 12:11 pm

How to programmatically select a datagrid

Post by CAsba » Mon May 12, 2025 12:35 pm

HI all,
I have three dg's on a card. I can select a dg by clicking on it and then using up and down arrows to select a line. But, I want to enable the user to use the keyboard instead of the mouse. Is it possible to programmatically 'click' on the dg to enable the user to use the up and down keys ?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to programmatically select a datagrid

Post by dunbarx » Mon May 12, 2025 2:34 pm

Hi,

You could

Code: Select all

click at the loc of grp "yourDatagrid".
.
That would focus on the DG you want. But once you get there, how do you navigate to the "cell" or row you are interested in? Something like:

Code: Select all

   click at the loc of fld "col 1 0002" of grp "yourDatagrid"
   click at the loc of fld "col 1 0002" of grp "yourDatagrid"
That will at least get you to the second row. I bet if you played around a bit, you could get to a particular field, in the same way as if you double-clicked on that field.

Obviously I am no expert with DG's, though I do use them now and then.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to programmatically select a datagrid

Post by FourthWorld » Tue May 13, 2025 1:37 am

See the dgHilitedLines property.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

CAsba
Posts: 423
Joined: Fri Sep 30, 2022 12:11 pm

Re: How to programmatically select a datagrid

Post by CAsba » Wed May 14, 2025 9:50 am

Many thanks.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to programmatically select a datagrid

Post by dunbarx » Wed May 14, 2025 1:48 pm

Richard.

A problem. Although something like:

Code: Select all

set the DGHilitedLines of grp 1 to 3
works fine, it does not "select" the DG itself.That means that the arrowKeys are not able to do any navigation. Clicking on a line in the DG does.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to programmatically select a datagrid

Post by FourthWorld » Wed May 14, 2025 4:06 pm

dunbarx wrote:
Wed May 14, 2025 1:48 pm
Richard.

A problem. Although something like:

Code: Select all

set the DGHilitedLines of grp 1 to 3
works fine, it does not "select" the DG itself.That means that the arrowKeys are not able to do any navigation. Clicking on a line in the DG does.
If he doesn't want the user to have to click on an object he wants to have focus, he'll have to handle events at the card or stack level and set the focus in response to the arrow key handler there.

The focus can be set with the "focus" command.
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: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to programmatically select a datagrid

Post by dunbarx » Wed May 14, 2025 5:13 pm

Richard.

Setting the focus of a DG does not, er, focus it. In fact, if I have a card with a single field, focussing on it:

Code: Select all

focus on fld 1
does not do what the dictionary says it does, which to, er, focus on it and place a blinking cursor there. I have to click on the field to do that.

I never use the focus command. This may be a good reason to still not.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to programmatically select a datagrid

Post by dunbarx » Wed May 14, 2025 5:17 pm

AHA.

It does work from a button, just not from the message box. I see a short-lived "blink" in the fld from msg, but nothing changes.

Craig

PaulDaMacMan
Posts: 683
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: How to programmatically select a datagrid

Post by PaulDaMacMan » Thu May 15, 2025 3:06 am

dunbarx wrote:
Wed May 14, 2025 5:13 pm
Richard.

Setting the focus of a DG does not, er, focus it. In fact, if I have a card with a single field, focussing on it:

Code: Select all

focus on fld 1
does not do what the dictionary says it does, which to, er, focus on it and place a blinking cursor there. I have to click on the field to do that.

I never use the focus command. This may be a good reason to still not.

Craig
Focusing and selecting are two different things.

I usually do this as a combo something like:

Code: Select all

focus on fld 1
select char -1 to -1 of fld 1
I'm usually doing that with list fields so..

Code: Select all

select line 1 of fld 1
The Datagid is different animal, a library / template stack with groups thing, so I think you would need to add to the chunk expression with something like 'select char tMyCharPosition+1 of fld 1 of group tMyDataGrid'.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 683
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: How to programmatically select a datagrid

Post by PaulDaMacMan » Thu May 15, 2025 3:15 am

I definitely have a working example somewhere, of a Datagrid that's entirely navigable from a keyboard, arrowKeys, tabKey etc. I'll try to dig it out and post it if I get a minute.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

Post Reply