如何在 wxWidgets 中从 wxTextCtrl 中删除焦点

How to remove focus from wxTextCtrl in wxWidgets

本文关键字:删除 焦点 wxTextCtrl 中从 wxWidgets      更新时间:2023-10-16

我在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的连接。

每次控件获得或失去焦点时,都会触发焦点事件,并且处理程序将开始操作。

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

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

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