如何从文本编辑对象检测焦点丢失? [英] How do I detect focus loss from a text edit object?

查看:199
本文介绍了如何从文本编辑对象检测焦点丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试在MATLAB中创建GUI。到目前为止,我还没有找到一种方法来检测焦点从文本编辑移动到其他对象。我需要这样的功能,所以我可以现场测试用户输入,如果输入形式不正确,文本编辑的背景颜色会变成红色。



换句话说,最终用户能够在文本编辑中编写表达式,然后按Tab键移动到下一个文本编辑,同时在第一个文本中看到一个红色的背景文本编辑的情况下,输入的一些问题。

我已经想到了几个替代方案来检查用户输入,但他们不像上面那么方便。如何实现这样的功能?

解决方案

当您按Tab键将焦点从可编辑文本框移动到另一个 uicontrol 对象,回调函数。因此,您只需将代码用于检查文本并提醒用户在可编辑文本uicontrol的回调函数中存在问题。



请注意文档指出,uicontrol的回调也将在这些其他条件:


  • 单击另一个组件,菜单栏或GUI的背景。


  • 对于单行可编辑文本框,请按 Enter

  • Ctrl + Enter


    例如,这里有一个非常简单的回调实现,如果字符串是'yes'或者'则将文本背景颜色设置为默认的灰度值。没有',或者如果字符串是其他字符,则为红色:

     函数回调_fcn(hSource,eventData)
    if is(get(hSource,'String'),{'yes','no'})
    set(hSource,'BackgroundColor',[0.941176 0.941176 0.941176]) ;
    else
    set(hSource,'BackgroundColor','r');
    end
    end


    This is my first attempt to create a GUI in MATLAB. I haven't been able so far to find a way to detect when focus is moved from a text edit to some other object. I need such functionality so I can test "on the spot" the user input and change the text edit's background color to red, if the input is formed in an incorrect way.

    In other words, it would be very convenient for the end-user to be able to write his expression in a text edit, then press tab to move to the next text edit, and at the same time see a red background in the first text edit in case of some problem with the input.

    I have thought of several alternatives to check the user input but they are not as convenient as the above. How might I implement something like this?

    解决方案

    When you press tab to move the focus from an editable text box to another uicontrol object, the callback function of the editable text box will be invoked. So, you would just have to put the code for checking the text and alerting the user to a problem in the callback function of your editable text uicontrol.

    Note that the documentation states that the callback for a uicontrol will also be invoked under these other conditions:

    • Clicking another component, the menu bar, or the background of the GUI.

    • For a single line editable text box, pressing Enter.

    • For a multiline editable text box, pressing Ctrl+Enter.

    For example, here's a very simple callback implementation which will set the text background color to the default gray value if the string is either 'yes' or 'no', or red if the string is anything else:

    function callback_fcn(hSource, eventData)
      if ismember(get(hSource, 'String'), {'yes', 'no'})
        set(hSource, 'BackgroundColor', [0.941176 0.941176 0.941176]);
      else
        set(hSource, 'BackgroundColor', 'r');
      end
    end
    

    这篇关于如何从文本编辑对象检测焦点丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆