【问题标题】:How to use tab to move in a list of fields in Red language如何使用制表符在红色语言的字段列表中移动
【发布时间】:2017-10-25 15:38:18
【问题描述】:

这类似于我之前的问题 (Using TAB to move between 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]
]

focusnumnone 给出。问题出在哪里,如何解决。

【问题讨论】:

    标签: user-interface rebol red


    【解决方案1】:

    两个修改使您的脚本工作

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

    您需要的是“面孔”,而不是指向面孔的词语。

    handle-key: function [e] [ 
    

    ->

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

    function 自动使所有的 set-words 本地化。因此局部 focusnum 没有初始化值,但全局 focusnum 仍然有起始值。动态查找focusnum应该可以解决“干扰”。可能有更有效的方法来做到这一点。

    这里是具有正确格式

    的几乎最终版本
    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]
    ]
    

    【讨论】:

    • 是的,它可以工作,但是如果通过单击鼠标手动选择某个字段,一切都会受到干扰。有什么办法可以纠正这个问题?我无法让findselect fldlist win/selected 为这个工作。
    • 这很好用。伟大的。我希望这个功能会出现在语言本身的未来版本中。
    【解决方案2】:

    我对代码做了一些进一步的简化:

    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函数。

    【讨论】:

    • handle-focus 是不需要的,正如我最初在回答中所展示的那样,但是与焦点更改时设置的解决方案相比,每次击键时设置 focusnum 的效率较低。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多