views:

1374

answers:

3

Hello,

I have a simple WPF application where user is able to minimize my application window.

After user restores Window from minimized state I need to set focus to certain TextBox.

If user before minimizing Window has not changed focus, then after restoring application everything is fine.

But problem comes when user has changed focused.

My Window has Activated event handler. And the code is following:

private void Window_Activated(object sender, EventArgs e)
{
   if (isFullView)
       tbSearch.Focus();
   else
       tbSearch2.Focus();            
}

After Window is restored from minimized state, event handler gets fired, but TextBox tbSearch does not recieve focus.

I'm I doing something wrong?

Thank You!

+1  A: 

Hard to say without knowing what isFullView is set to, but I can tell you that Focus() sets the logical focus, not the keyboard focus. The control will only have keyboard focus if its focus scope is the active focus scope.

Please read this article for more information on focus in WPF.

HTH, Kent

Kent Boogaart
At moment isFullView = trueMy current solution calls tbSearch.Focus() on Minimize button click
Daniil Harik
A: 

Try making sure if the TextBox is visible by the time that handler gets called. Maybe you have some triggers or some other things that show the TextBox later than focus is being set.

arconaut
A: 

you need to recognize prev. win. state. see this post with info: http://blogs.microsoft.co.il/blogs/maxim/archive/2009/12/24/daily-tip-how-to-activate-minimized-window-form.aspx.

tom