I got a strange problem with a TextBox, which is in an Xpander in an XpanderList in a custom UserControl.
(Using C#, .NET 4, Visual Studio 2010, WinForms, Windows 7.)

I am making a simple "auto-complete", by starting a Timer in my TextBox TextChanged event.
If this timer fires a tick before next TextChanged event, I interpret this as the user stopped typing,
and I go doing a search in my data. After the search, I do a lot of GUI stuff to display the result.

Now, this works fine. But afterwards, my TextBox has not focus anymore!
No cursor; I can't type there without clicking in the TextBox.

I tried TextBox.Focus() and TextBox.Select() without luck, but the funny thing is that two alt-tabs helps!
Also breaking in the debugger and continuing helps: the cursor gets into the TextBox, and I can type there.

So it could look like my application or form lost focus, but how? I did not activate any other application.
Is it something with the fact that I do all this from a Timer tick event? Does it mess up the event dispatching?
Or is Xpander and XpanderList preventing something? I have the Xpander expanded and visible throughout this scenario.

Thanks for any help with this strange issue!

Recommended Answers

All 3 Replies

I think the issue lies in here:

After the search, I do a lot of GUI stuff to display the result.

What you do here probably holds the reason the texbox loses focus and it takes two alt-tabs to get back to it. What exactly do you do then?

Thanks for your reply hericles!

What I do after the timer tick is: pull up a progress bar, search, update a grid and pull down the progress bar.

I can understand if this changes focus, but I can't understand why I can't set the focus back with Focus() or Select().

And even more strange is that alt-tabbing or debugger break+continue gives it back to the TextBox. It's like if the TextBox has the focus all the time, but my applicationor Form loses it...

Don't know if this is of general interest, but I found the problem:
When searching I disabled + enabled the TextBox.
Apparently, the disable had effect before search returned, but not the enable.
I assume the enable is still in the event queue, until I do something else (like alt-tab or debug break+continue).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.