views:

870

answers:

1

I have a user control that behaves similar to a tab control. The tab headers are UserControls 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 subcontrols, and I call myControl.Focus() from the MouseClick event, focus returns true.

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

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

Can someone explain this?

A: 

I believe that calling Focus() on a UserControl selects the first childcontrol of that UserControl. That would indeed explain your behaviour. This is also described here:

http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic32787.aspx

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

Razzie