gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:42 29 4
gpt4 key购买 nike

我正在为 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com