SetFocus not working in EN_CHANGE event

Started by Dan English, August 19, 2011, 01:29:44 PM

Previous topic - Next topic

Dan English

I have a very small chunk of code that fires in the EN_CHANGE event of a text box.

I only need it to do two things -- 1) update a label elsewhere on the form, and 2) set the focus to another text box.

Now updating a label elsewhere works fine.  Setting the focus to another text box will not work.  Is there something I'm missing that might cause the SetFocus to get ignored?  I know I'm passing it the correct HWND...

Paul Squires

Hi Dan,

I am not at my development machine to test but you can try this....

Create a user message and then PostMessage it during the EN_CHANGE.

%MSG_USER_SETFOCUS = %WM_USER + 100

In the EN_CHANGE.....

PostMessage HWND_FORM1, %MSG_USER_SETFOCUS, <HANDLE OF CONTROL TO SETFOCUS>, 0

In the Form1 CUSTOM, respond to the user message.

Select Case wMsg
   Case %MSG_USER_SETFOCUS
      SetFocus wParam
End Select

The reason it is probably not working for you is because the focus is staying with the current TextBox until after the EN_CHANGE notification continues to fire. By using PostMessage, you are allowing the EN_CHANGE to finish and then you have control over setting the focus without having to worry about the TextBox stealing it back from you.
Paul Squires
PlanetSquires Software