Craigger
Craigger

Reputation: 91

UserControl Focus Issue - Focus() sometimes returns false

I have a user control that behaves similar to a tab control. The tab headers are UserControl instances that override Paint events to make them look custom.

In order to leverage the Validating events on various controls on our tab pages, when the user clicks on the tab headers, we set the Focus to the TabHeader user control.

I've noticed that Control.Focus() returns false sometimes but the documentation does not say why Control.Focus() will ever return false other than that the control can't receive focus. But I don't know why.

Here's what I see: If my TabHeader UserControl does not contain any sub-controls, and I call myControl.Focus() from the MouseClick event, focus returns true.

If my TabHeader UserControl contains a sub-control and I call myControl.Focus() from the MouseClick event, focus returns false.

If my TabHeader UserControl contains a sub-control, and I call myControl.subControl.Focus() from the myControl.MouseClick event, focus returns true.

Can someone explain this?

Upvotes: 2

Views: 6465

Answers (2)

Razzie
Razzie

Reputation: 31212

Calling Focus() on a UserControl selects the first childcontrol of that UserControl. That would indeed explain your behaviour.

You can try to call Select() instead of Focus() and see if that works.

Upvotes: 5

James Poulose
James Poulose

Reputation: 3835

Did you try "Control.ContainsFocus"? This is supposed to return true if any of the child controls have focus on it. MSDN Link

Thanks, James

Upvotes: 1

Related Questions