[SOLVED]-CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

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
Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 365
Joined: Mon Jun 10, 2013 1:32 pm

[SOLVED]-CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by Lagi Pittas » Tue Feb 17, 2015 6:26 pm

HI

WE have all these handlers we can "override" like closefield , exitfield, focusin, focusout , enterinfield (which is actually the RETURN key ) enter is the key on the numeric keypad.

They are not consistent as to when they are called - Ive tried to call closefield from the datagrid script, the column behaviour and it doesn't get called. ExitField gets called in the field editor (from revdata)n when I attach that behavior to a button a point the grid to it. on EscapeKey works in the same custom field editor but on closefiled doesnt.

Now I realise that somehow the datagrid takes control but when you have these lines in the default behavior :-

on closeField
## Don't delete editor within same message
send "DeleteFieldEditor" to the dgControl of me in 0 seconds
end closeField

Then you'n expect it to be called.

My question is I want to validate the data in a grid column (say date, currency etc)

But I don't want to have the call inside 3 or 4 (or more) handlers - which is the most high level handler that can be called when entering or leaving a cell and either stopping the exit/losing focus or directing the focus to another object.

i have had to put code IN ESCAPEKEY, EXITFIELD, RETURNINFIELD, arrowkeys and tabkey just to make sure I haven't missed anything.
This is too hit and miss for my liking.

What we need is a command that we can put into the messagepath that always gets called when the focus is TRIYNG to leave a cell and you either pass back a field/cell number or a object to put focus on.

In foxpro we had a very consistent system

Valid()
When()
LostFocus()
GotFocus()

Just to be more specific - I know that you Gurus out there (Trevor, Klause, Jaq, Mweider, Zryip, Simon, Bangkok, Darbx .... sorry if I left anybody out, this was just from memory) could give me a few pointers - Im assuming the DeleteFieldOpenNext is probably where I should be looking is that right?

Hope that's clear - It would also

Oh and any specific Gotchas that anyone else has found would be useful to me (and all the other beginners).


KIndest Regards Lagi
Last edited by Lagi Pittas on Wed Feb 18, 2015 8:46 pm, edited 1 time in total.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by bangkok » Tue Feb 17, 2015 8:45 pm

Follow this lesson :

http://lessons.runrev.com/m/datagrid/l/ ... -to-a-cell

Then, inside the button script add :

Code: Select all

on ExitFieldEditor pFieldEditor
   answer "no change"
end ExitFieldEditor

on CloseFieldEditor pFieldEditor
   put the text of pFieldEditor into tText
   answer "You have typed "&tText
end CloseFieldEditor
Or if you want to apply this behavior to only column : go property inspector, switch to the Columns pane and click on the Column Behavior button.

And paste the same script inside.

Details of messages sent :
http://lessons.runrev.com/m/datagrid/l/ ... a-grid-api

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Location: Palo Alto, CA put williamdjamieso into tEmail / put n@gmail.com after tEmail/ revmail tEmail
Contact:

Re: CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by William Jamieson » Tue Feb 17, 2015 8:55 pm

Hello Lagi. I am not a super professional like the names you mentioned, nor do I understand what your goal is in figuring this out. But from what you mentioned I may have a couple things that could help you out.

First, enterInField is specifically for the button near the number pad on your keyboard that says "Enter". The returnInField i s used for the one above the shift key and any of the return keys that are available on mobile such as "search", "Go", "Enter", etc.

Second, There are two commands that get sent before losing focus from a field. One is exitField. [EDIT BY JACQUE: Exit field is sent when the user makes a change to the content and closeField is sent when the content is unchanged]. If you use breakpoints to stop the script editor, you will see that the closeField and exitField commands get sent before the new field is focused which gives you time to say for example, refocus the current field if you wanted to by writing

on ExitField
focus on me
end ExitField

That code might make it so you can never leave the field and would probably have zero application but i hope you get the point.

Third, Scripts can be places in the objects in your datagrid in the template row so that script manipulation can occur before it even gets to the datagrid so that you can test your code with no datagrid interference as they do get tricky sometimes. You can also put code in the template row group and use that instead of the row behavior, which will effectively do the same thing. But just know you have options to play with.

Fourth, See if focusIn or FocusOut help you in your situation at all. I havent really worked with those much but those are more options for you.

Fifth, from how your sentence was structured I got the impression that you might have been trying to call a Livecode message and use that to manipulate the field. I say that because of this line you wrote
Ive tried to call closefield from the datagrid script
. Just know that you cannot call Livecode made messages such as CloseField. To close a field one must use a command such as "focus on field "ThisIsTheOtherField" of me" or 'focus on nothing' These commands will automatically change send the closeField, ExitField, OpenField appropriately and reliably.

So five tips, you may not need any of them. But I hope at least one of them helps you with your project.

-Will
Last edited by William Jamieson on Wed Feb 18, 2015 9:27 pm, edited 1 time in total.

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

Re: CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by dunbarx » Tue Feb 17, 2015 10:56 pm

Hey, Klaus, this guy is wondering why dataGrids are hard to figure out. Mmmphhh...

Oh. Hi. Ahem. You are not alone. Know that DG's work fine, and contain enormous power.

Make a simple DG, give it a little content, and open the message watcher. Suppress everything but messages that are handled. Now put this in the group script:

Code: Select all

on closefield
   put "CloseField" && random(99)
end closefield

on dgMouseUp
   put "DGMouseUp" && random(999)
end dgMouseUp
Change the contents of a field, and click in another field. Note that the "closeField" handler does not fire, even though you see it in the message watcher. Note that the "dgMouseUp" message is indeed detected in the message box. The DG has a lot going on inside, and it traps "closeField". But it allowed dgMouseUp to pass to your group script. That is because this message was not used internally.

Now put this into the group script:

Code: Select all

on selectionChanged
    put " SC" && random(999)
end selectionChanged
Again, can you use this instead of "closeField"? Is it robust enough to be a valid substitute? Should you have to worry about this sort of thing? Good luck.

Craig Newman

Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 365
Joined: Mon Jun 10, 2013 1:32 pm

Re: CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by Lagi Pittas » Wed Feb 18, 2015 5:32 pm

Hi

Thank you for all your help.

I created a small test stack and "played" for about 4 hours - I have nothing better to do of course. :wink:


Here are a few of the handlers I played with

Code: Select all

on RawKeyDown
   local lnRow, lcContents, laRowData
   global lcTheData
   put the dgHilitedLines of group me into lnRow
   put the dgDataOfLine[lnRow] of group me into laRowData
   Put "RawkeyDown " & laRowData["number"]   into line  (the number of lines in lcTheData + 1) of lcTheData
   pass RawKeyDown
end RawKeyDown

on TextChanged
   local lnRow, lcContents, laRowData
   global lcTheData
   put the dgHilitedLines of group me into lnRow
   put the dgDataOfLine[lnRow] of group me into laRowData
   Put "TextChanged " & laRowData["number"]  into line  (the number of lines in lcTheData + 1) of lcTheData
   pass TextChanged
end TextChanged

on RawKeyUp
   local lnRow, lcContents, laRowData
   global lcTheData
   put the dgHilitedLines of group me into lnRow
   put the dgDataOfLine[lnRow] of group me into laRowData
   Put "RawkeyUp " & laRowData["number"]  into line  (the number of lines in lcTheData + 1) of lcTheData
  
end RawKeyUp

on mouseUp pMouseBtnNum
   local lcColData, lcColumn
   global lcTheData
   
   put the dgColumn of the target into lcColumn
   if lcColumn is not empty then
      put GetDataOfIndex(the dgIndex of the target, lcColumn) into lcColData
      Put "Mouseup  >>> " &  lcColData  into line  (the number of lines in lcTheData + 1) of lcTheData
   end if
end mouseUp
I have a scrolling field and bunched up the messages and showed them.

Anyway unless there is something else that are have missed here is the problem.

ALL of these fire on keystroke (rawkeydown is needed for the non printable) but however I access the data in the cell it ALWAYS the data as it was when the cell/column got focus.
When you leave the cell and go to another cell by pressing tab or enter rawkeyup gives you the data as it is - but its now to late we have moved to the new cell.

"on SelectionChanged" just tells you you have moved to a new row/column but it is only in a non editing mode.

What I want to be able to do is take control of the editing so I can control all input BEFORE final validation and go to any object/target I want.

I wrote a routine below

Code: Select all

function CurrencyValidation pAmount, pTheKey
   constant kREG = "^[0-9]{1,5}+(\.[0-9]{1,2})$"
   local lcText
   
   put pAmount into lctext
   
   if pTheKey is "." then
      put lcText & ".00" into lcText -- The Regex checks the whole string so 123  would not validate
   else
      put lcText & pTheKey into lcText 
   end if 
   
   if "." is not in lcText then put lcText & ".00" into lcText -- Still typing left side of Decimal
   if matchText(lcText,kREG) then
      return TRUE
   end if
           
   beep
   return FALSE
   
end CurrencyValidation
Which works 90% the way I want it to work. The problem is when I go into the field the text isn't selected - I need it to select all the text and if they hit any printable key the field is blanked and so they can start typing.


Ok Update -- while writing this I had a flash of inspiration and tried putting into the TEXTChanged event and the RAWKEYUP event the code "GET THE TEXT of the TARGET" and "GET THE SELECTEDCHUNK of the TARGET" . So now I have the insertion point and the actual data -- we're making progress.

So I added the focus on me / focus on the target (tried both) in the exit field event . This stopped it from going to the next cell but stayed on the current cell but it was not open for editing.
i even added a "send mouse double up to me" and all permutations thereof (well my head is spinning so it was most :wink: )

so I added the code below - It has the correct line and key (column name) but still won't do it.

Code: Select all

on ExitFieldEditor pFieldEditor
    put the dgHilitedLines of group "datagrid 1" into theLine 
     put the dgcolumn of the target into  theColumn 
    
   send "EditKeyOfIndex theColumn, theline" to the group "datagrid 1" in 30

   focus on the target
  
end ExitFieldEditor
Basically what I need now is a way to stay on the current field in edit mode with the insertion point at the end

Without "focus on the target" it goes to the next field whatever I do.

I'm stumped - but bloody close

Thanks everybody for your help (btw it's now been 6 hours)

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

Re: CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by dunbarx » Wed Feb 18, 2015 6:16 pm

Not sure this addresses your problem.

When you click on a "field" in a data grid, the "dgMouseUp" message is sent and may be trapped in the group script, as we saw, and the field is named, say, "Col 1 0002", which is column 1, row 2 It is simple to:

Code: Select all

select fld "col 1 0002"
Now go to the DG guide and on page 224 see if you can figure out how to use the commands like "editCell" or editFieldText". When do figure that out, please write back and tell *me*.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7236
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by jacque » Wed Feb 18, 2015 6:17 pm

William Jamieson wrote:Second, There are two commands that get sent before losing focus from a field. One is exitField. This is when the user clicks of the field and is not focusing on another field. The other is closeField. This is when the user is trying to focus in another field.


Not exactly. ExitField is sent when the field loses focus but its content has not changed. CloseField is sent when the field loses focus and its content has changed. The destination or the next target is immaterial.
Just know that you cannot call Livecode made messages such as CloseField. To close a field one must use a command such as "focus on field "ThisIsTheOtherField" of me" or 'focus on nothing' These commands will automatically change send the closeField, ExitField, OpenField appropriately and reliably.
Just a little clarification: You can't make a field close by issuing the message that is sent after it closes, which applies to any message that occurs as a result of a user action. But you can insert any LC system message into a handler and it will traverse the message hierarchy normally.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 365
Joined: Mon Jun 10, 2013 1:32 pm

Re: CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by Lagi Pittas » Wed Feb 18, 2015 6:47 pm

HI

I was not calling closefiled I meant I was trying to trap it.

Actually its with the closefieldeditor I was trapping for the grid.

I put this code

Code: Select all

      put the dgHilitedLines of group "datagrid 1" into theLine  
     put the dgcolumn of the target into  theColumn 
     send "EditKeyOfIndex theColumn, theline" to the group "datagrid 1" in 30
Into the closefieldeditor event as well, with and without the setfocus to me / target but I was playing with the "exitfield" event because i could just type tab without typing anything else (lazy? .. maybe )

I thought the EditKeyoFindex calls the standard editext routine that will open for editing - otherwise it seems i'm going to have to use 8000 lines of datagrid code for each column - or so it seems.

Maybe I need to go into the revdatagrid library and add an extra routine there if I ever get out alive -
"Oh the pain, the pain!" -- Dr Zakary Smith.
Just a suggestion

The revdatagrid library is all there "opensource" couldn't we start a thread where some of the showstoppers can be brought up[ and added to our "own version"

The datagrid is VERY powerful in reality the fact that you (Trevor) could code it in Livecode shows the power of livecode (and the tenacity of Trevor Devore) but for 200 ish lines more we could save a lot of questions on the forum - How about it Gurus?


Regards Lagi

Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 365
Joined: Mon Jun 10, 2013 1:32 pm

[SOLVED] CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by Lagi Pittas » Wed Feb 18, 2015 8:13 pm

Hi

After another couple of hours where my meticulous naming conventions lost the plot ...

And I mixed up EditCellofIndex for editcell (quite why index is different to the row when I only loaded 4 rows in the content field I'll never know, unless its to do with headers sorting the columns).

Anyway here is the code that will be my starting position for moving to any cell depending on what is in the field

Code: Select all

on ExitFieldEditor pFieldEditor
   put the dgHilitedLines of group "datagrid 1" into theLine 
   put the dgcolumn of the target into  theColumn 
   
    put "Exit: " & ttext
   if txext contains "top" then 
      send "EditCell string, 3" to the group "datagrid 1" in 60
   end if
   
    if txext contains "bot" then 
      send "EditCell string, 4" to the group "datagrid 1" in 60
   end if
   
   
end ExitFieldEditor

on CloseFieldEditor pFieldEditor
   put the text of pFieldEditor into tText
   
    put the dgHilitedLines of group "datagrid 1" into theLine 
    put the dgcolumn of the target into  theColumn 
    
    put "Close: " & ttext
    if  ttext contains "date" then
       send "EditCell " & "date, " & theline to the group "datagrid 1" in 60
    end if 
    
    if ttext contains "top" then 
      send "EditCell string, 1" to the group "datagrid 1" in 60
   end if
   
    if ttext contains "bot" then 
        send "EditCell string, 4" to the group "datagrid 1"  in 60
   end if
    
      if  ttext contains "string" then
       send "EditCell string, " & theline to the group "datagrid 1" 
    end if 

   pass CloseFieldEditor
end CloseFieldEditor

Notice the owrds bot, string, date etc are my way of testing that I can move around depending on what is in the cell.

I can now make the grid focus wherever I want or move anywhere in the grid - which is a bonus

Still will need to work out if I need to pass exitfieldeditor and closefieldeditor - probably not.

Now this is in the "number" column behavior, so my idea now is to put this in the grid script with my other code and have a switch statement for specific columns.

Ie if the column contains dates it calls a validate date routine, if numbers validate currency etc.

The actual key by key validation will be done in the textchanged event because now I have the insertion point and the actual content I can overide the "editing keys" like delete, backspace etc.
If at first you don't succeed ... Cheat!!
-- Lagi

and so I can go to bed with "The Eternal Sunshine of the Spotless Mind " :D

Thanks everyone
Last edited by Lagi Pittas on Thu Feb 19, 2015 12:21 am, edited 2 times in total.

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Location: Palo Alto, CA put williamdjamieso into tEmail / put n@gmail.com after tEmail/ revmail tEmail
Contact:

Re: [SOLVED]-CLOSEFIELD EXITFIELD FOCUSIN GRID VALIDATION

Post by William Jamieson » Wed Feb 18, 2015 9:23 pm

Yep Jacque! You are absolutely right about that. Sorry about the confusion of the ExitField and CloseField. Look at Jacques response for more clarity.

Glad you were able to get this solved Lagi!

-Will

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”