【问题标题】:How to remove focus from wxTextCtrl in wxWidgets如何从 wxWidgets 中的 wxTextCtrl 移除焦点
【发布时间】:2018-08-08 17:20:05
【问题描述】:

我在wxWidgets 中将wxEVT_SET_FOCUS 用于wxTextCtrl。我需要做的是,当用户单击 textctrl 时,我必须打开一个新窗口并从 textctrl 中移除焦点,以便 FocusHandler 函数只执行一次。有什么功能可以从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?
 }

基本上,一旦打开新窗口,我想停止处理函数的多次执行,而无需断开 wxeVT_SET_FOCUS 与 wxTextCtrl 的连接。

【问题讨论】:

    标签: c++ event-handling wxwidgets


    【解决方案1】:

    每次控件获得或失去焦点时,都会触发焦点事件,并且您的处理程序会开始行动。

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

    我们使用标志来处理重入,它告诉我们是否处于重入情况。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      相关资源
      最近更新 更多