c++ - 如何从 wxWidgets 中的 wxTextCtrl 移除焦点

标签 c++ event-handling wxwidgets

我正在为 wxWidgets 中的 wxTextCtrl 使用 wxEVT_SET_FOCUS。我需要做的是当用户单击 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 的连接。

最佳答案

每次控件获得或失去焦点时,都会触发焦点事件,并且您的处理程序会起作用。

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

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

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

关于c++ - 如何从 wxWidgets 中的 wxTextCtrl 移除焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51752299/

相关文章:

c++ - 我可以忽略 "Intellisense: (E0028) expression must have a constant value"吗?

c++ - 在Cmake中构建Assimp时出错

c++ - 如何使用 zlib 解压 gzipstream

excel - 将单击 VBA 函数分配给 Excel 用户窗体上动态创建的按钮

python - wxPython 中的 "NotImplementedError: wxGCDC is not available on this platform"

c++ - 尝试删除二叉树中的节点时双重释放或损坏(out)

javascript - 在特定视口(viewport)宽度以下禁用事件处理程序

javascript - 如何以正确的方式包含这个js

c++ - ( __ printf __ ) 在 DLL (programfiles(x86)\codeblocks\mingw\bin\as.exe) 中找不到

c++ - wxWidgets中的 'delete'和 'destroy'有什么区别?