Issues with Text Control and Focus Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MichaelWard
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jul 13, 2006 9:36 pm

Issues with Text Control and Focus

Post by MichaelWard »

I am trying to do something simple that I have no problem doing in MFC. I want to evaluate a text control when it loses focus. IE if you click somewhere else the text control will get evaluated and then if their is a problem it will force the focus back to the text control and highlight the text.

I believe I should be able to do it using the EVT_KILL_FOCUS() but this event only happens within the control and does not propagate. I was able to get it to work but feels more like a hack then a solution.

I had to make a subclass of wxTextCtrl and handle the focus event there. Inside that focus event I sent an event to the parent class which would handle the validation and reset the focus if it must. The problem here is that if the entire window loses focus then all the text controls fire off this event. So I had to add more code in the EVT_KILL_FOCUS to check if the parent still has focus. I don't really think this is a good way to do this. Is their not a simple event handlers for controls gaining and losing focus that can be caught by their container (parent)?

There was one other idea that came up and that was to create an event handler to handle this focus and attach it to the text controls and have this event handler notify the parent of the events. I am just wondering if their is a simple way of doing this or is this a flaw in wxWidgets.

Thankx
Jamie
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 205
Joined: Wed Mar 30, 2005 10:56 pm

Post by Jamie »

Have you tried using wxUpdateUIEvent?

You could do your evaluation logic there and use event.GetEventObject()->SetFocus(); to reset the focus if needed.
MichaelWard
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jul 13, 2006 9:36 pm

Post by MichaelWard »

I wanted to avoid using wxUpdateUIEvent cause its called often. Plus If I ever wanted to implement validation, you generally check if the value and then if it is incorrect display a message and then highlight the text in the text box control. If I used the UpdateUI events the text control can receive an update while the user is typing text which could evaluate to an incorrect value in the middle of their typing which would aggrevate a user if a messagebox continued to pop up or if their text got highlighted so when they pressed the next key everything got erased.

I was hoping that wxWidgets and a simple focus events that I could use and that for some reason I could not find them in the API, or I was over complicating things.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

i'm not quite sure if this is what you want, but instead of subclassing the wxTextCtrl you could use Eventhandler::Connect(). This will allow you to explicitly name the event handler that's supposed to handle this event. E.g. you could send all killfocus events to your parent window.

Code: Select all

wxTextCtrl *yourtextcontrol=new wxTextCtrl(...);
yourtextcontrol->Connect(wxEVT_KILL_FOCUS, yourParentFrame::OnKillFocus, NULL, (yourParentFrame *)frame_pointer);
Use the source, Luke!
MichaelWard
Knows some wx things
Knows some wx things
Posts: 38
Joined: Thu Jul 13, 2006 9:36 pm

Post by MichaelWard »

Thanks. This appears to be an easier way to do it. Still have some minor issues but I got around them.

FYI,

Code: Select all

wxTextCtrl* myTextCtrl= new wxTextCtrl( itemPanel1, ID_TEXT_TEST, _T(""), wxDefaultPosition, wxDefaultSize, 0 );

itemBoxSizer2->Add(itemTextCtrl4, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
   
itemTextCtrl4->Connect(wxEVT_KILL_FOCUS,wxFocusEventHandler(MyPanel::OnTextLostFocus),NULL,this);
Only problem is the text control loses focus when the window loses focus. I fixed this by doing the following,

Code: Select all

OnTextLostFocus(wxFocusEvent &event)
{
   wxWindow *win = wxWindow::FindFocus();
   if( win != NULL)
   {

            // DO my Validation...
    }

}
Thanks Again.
Post Reply