红语如何使用tab在字段列表中移动

How to use tab to move in a list of fields in Red language

这类似于我之前的问题 ()。由于我有很多字段,所以我想使用一个字段列表。但是,以下代码不起作用:

Red []

view [
    text "Value of x:"  f1: field "" on-key [handle-key event] return
    text "Value of y:"  f2: field "" on-key [handle-key event] return
    text "Third: " f3:  field "" on-key [handle-key event] return
    text "Fourth:" f4:  field "" on-key [handle-key event] return
    text "Read Sum:"    tt: text ""  on-key [handle-key event] return
    do[
        fldlist: [f1 f2 f3 f4]
        focusnum: 1
        maxfocusnum: length? fldlist
        handle-key: function [e] [
            print rejoin ["focusnum = " focusnum]   ; OUTPUT: "focusnum = none"
            k: e/key
            if k = tab [
                either e/shift? 
                    [   focusnum: focusnum - 1
                        if focusnum < 1 [focusnum: maxfocusnum]
                        win/selected: fldlist/:focusnum]
                    [   focusnum: focusnum + 1
                        if focusnum > maxfocusnum [focusnum: 1]
                        win/selected: fldlist/:focusnum]
                        ] ] ]

    button "Calculate" [
        tt/text: to-string ((to-integer f1/text) + (to-integer f2/text))      
    ]
    button "Quit" [quit]  
    do [win: self win/selected: f1]
]

focusnum 给出为 none。问题出在哪里,如何解决。

两次修改使您的脚本正常工作

fldlist: [f1 f2 f3 f4] -> fldlist: reduce [f1 f2 f3 f4

您需要 'faces' 而不是指向面孔的文字。

handle-key: function [e] [ 

->

 handle-key: func [e] [
     focusnum: index? find fldlist win/selected

function 自动使所有设置词本地化。因此局部的focusnum没有初始化值,但是全局的focusnum还是有起始值。动态查找 focusnum 应该可以解决 'disturbance'。可能有更有效的方法。

这里几乎是最终版本,具有适当的格式

view [
    text "Value of x:"  f1: field "" on-key [handle-key event]  on-focus [handle-focus face event] return
    text "Value of y:"  f2: field "" on-key [handle-key event] on-focus [handle-focus face event] return
    text "Third: " f3:  field "" on-key [handle-key event] on-focus [handle-focus face event] return
    text "Fourth:" f4:  field "" on-key [handle-key event] on-focus [handle-focus face event] return
    text "Read Sum:"    tt: text ""  on-key [handle-key event] return
    do[
        fldlist:  reduce [f1 f2 f3 f4]
        focusnum: 1
        maxfocusnum: length? fldlist
        handle-key: func [e] [
            print rejoin ["focusnum = " focusnum]  
            k: e/key
            if k = tab [
                either e/shift?   [   
                    focusnum: focusnum - 1
                    if focusnum < 1 [focusnum: maxfocusnum]
                    set-focus  fldlist/:focusnum
                ] [   
                    focusnum: focusnum + 1
                    if focusnum > maxfocusnum [focusnum: 1]
                    set-focus  fldlist/:focusnum
                ]
            ]
        ] 
        handle-focus: func [f e] [ probe focusnum: index? find fldlist f]
    ]
    button "Calculate" [
        tt/data:  f1/data + f2/data      
    ]
    button "Quit" [quit]  
    do [win: self win/selected: f1]
]

我进一步简化了代码:

view [
    text "Value of x:"  f1: field "" on-key [handle-key event] return
    text "Value of y:"  f2: field "" on-key [handle-key event] return
    text "Third: " f3:  field ""    on-key [handle-key event] return
    text "Fourth:" f4:  field ""    on-key [handle-key event] return
    text "Read Sum:"    tt: text "" on-key [handle-key event] return
    button "Calculate" [
        tt/data:  f1/data + f2/data   
    ]
    button "Quit" [quit]  

    do[ fldlist:  reduce [f1 f2 f3 f4]
        mafocusnumfocusnum: length? fldlist
        handle-key: func [e] [
            if e/key = tab [
                focusnum: index? find fldlist win/selected
                either e/shift?
                    [   focusnum: focusnum - 1    
                        if focusnum < 1 [focusnum: mafocusnumfocusnum] ]
                    [   focusnum: focusnum + 1    
                        if focusnum > mafocusnumfocusnum [focusnum: 1] ]
                set-focus fldlist/:focusnum 
            ]   
        ]
        win: self win/selected: f1
    ] 
]

这里不需要handle-focus函数。