简体   繁体   English

如何从wxWidgets中的wxTextCtrl移除焦点

[英]How to remove focus from wxTextCtrl in wxWidgets

I am using wxEVT_SET_FOCUS for wxTextCtrl in wxWidgets . 我在wxWidgets wxEVT_SET_FOCUS用于wxTextCtrl What I need to do is when user clicks textctrl I have to open a new window and remove focus from textctrl so that FocusHandler function will be executed only once. 我需要做的是,当用户单击textctrl时,我必须打开一个新窗口并从textctrl中删除焦点,以便FocusHandler函数仅执行一次。 Is there any function to remove focus from wxTextCtrl ? 有什么功能可以从wxTextCtrl移开焦点吗?

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?
 }

Basically I want to stop multiple executions of handler function once the new window is opened without disconnecting the wxeVT_SET_FOCUS from wxTextCtrl. 基本上,我想在打开新窗口后停止处理程序函数的多次执行,而又不从wxTextCtrl断开wxeVT_SET_FOCUS的连接。

Each time the control gains or looses the focus a focus event is fired, and your handler comes into action. 每次控件获得焦点或失去焦点时,都会触发焦点事件,并且处理程序将开始执行操作。

There are some causes for this focus event. 此焦点事件有一些原因。 The most cumbersome is when the new window is created and deleted, because it may generate its own focus events, and your handler may deal with them all. 最麻烦的是创建和删除新窗口时,因为它可能会生成自己的焦点事件,而您的处理程序可能会处理所有这些事件。 There's a reentrance issue. 有一个重入问题。

We deal with reentrance using a flag, which tell us if we are in a reentrance case. 我们使用标志来处理重入,该标志告诉我们是否处于重入案件中。

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM