首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在wxTextCtrl中消除wxWidgets中的焦点

如何在wxTextCtrl中消除wxWidgets中的焦点
EN

Stack Overflow用户
提问于 2018-08-08 17:20:05
回答 1查看 1.1K关注 0票数 1

我用wxEVT_SET_FOCUS表示wxTextCtrl in wxWidgets。我需要做的是,当用户单击textctrl时,我必须打开一个新窗口并从textctrl中移除焦点,以便只执行FocusHandler函数一次。是否有从wxTextCtrl中移除焦点的功能?

代码语言:javascript
复制
Using this connect event in constructor
//Connect Events
m_passwordText->Connect(wxEVT_SET_FOCUS, wxFocusEventHandler(MyFrame::OnPasswordTextBoxSetFocus), NULL, this);


 void MyFrame::OnPasswordTextBoxSetFocus(wxFocusEvent& event)
 {
       if(some condition is true)
           //code to open a new window

       event.Skip()
       // is there any option to remove focus from password textCtrl so that once a new window opens
       //I can remove focus from password and avoid executing this function again and again.
       // If new window opens for the first time, I perform the operation and close it
       // then also it opens again as control is still in password textbox. 
       //Is there any way to resolve this?
 }

基本上,我希望在打开新窗口后停止多次执行处理程序函数,而不将wxeVT_SET_FOCUS与wxTextCtrl断开连接。

EN

回答 1

Stack Overflow用户

发布于 2018-08-08 18:23:22

每次控件获取或松开焦点时,都会触发焦点事件,而您的处理程序就会开始操作。

这一焦点事件有一些原因。最麻烦的是在创建和删除新窗口时,因为它可能会生成自己的焦点事件,而您的处理程序可能会处理所有这些事件。有个重新进入的问题。

我们使用一个标志来处理重新进入,它告诉我们是否在重新进入的情况下。

代码语言:javascript
复制
void MyFrame::OnPasswordTextBoxSetFocus(wxFocusEvent& event)
{
    static bool selfFired = false; //our flag

    event.Skip(); //always allows default processing for focus-events

    if ( event.GetEventType() == wxEVT_KILL_FOCUS )
    {
         //Nothing to do. Action takes place with wxEVT_SET_FOCUS
         return;
    }

    //Deal with re-entrance
    if ( !selFired )
    {
         if (some condition)
         {
              selfFired = true;

              //Open a new window.
              //Pass 'this' to its ctor (or similar way) so that new window
              //is able to SetFocus() back to this control
              newWin = open a window...

              newWin->SetFocus(); //this may be avoided if the new window gains focus on its own
         }
    }
    else
    {
        //restore the flag
        selfFired = false;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51752299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档