views:

376

answers:

3

I'm creating a custom control derived from UserControl that I would like to set focus to.

The custom control contains a ComboBox control and I draw some strings beside it.

The ComboBox can receive focus, but I would like to be able to set focus to the UserControl itself. My OnPaint handler is set up to draw the control slightly differently if it has focus but I call CanFocus() from the parent form when I create my custom control and it returns false.

Is there a property or something to set?

A: 

From http://msdn.microsoft.com/en-us/library/system.windows.forms.control.canfocus.aspx

Remarks

In order for a control to receive input focus, the control must have a handle assigned to it, and the Visible and Enabled properties must both be set to true for both the control and all its parent controls, and the control must be a form or the control's outermost parent must be a form.

Ensure you have meet these prerequesits.

David Waters
Thanks for your response...I've confirmed that my control has a value for Handle. The Control is Visible and Enabled too and the Panel I display it inside of is Visible and Enabled as well...and it all appears on a Form...!That seems to meet the pre-requisites...I overrode OnGotFocus() and set a breakpoint but it never got hit. Also, I set up a handler in my form object for the control's GotFocus event and, once again, the code never got hit.Any other ideas...?
Sambo
+2  A: 

UserControl will fight you tooth and nail to avoid getting the focus. It has code that automatically passes the focus to a child control (if any) if it does get the focus. You'll at a minimum have to override WndProc() and trap the WM_SETFOCUS message. There might be other surgery needed, like ControlStyles.Selectable and the TabStop, TabIndex properties.

Your next issue is that UserControl won't respond meaningfully to, say, keyboard messages when it does has the focus. And mouse messages, you'll need to detect clicks on the UC background. And you'll need to override the painting so it is obvious to the user that it has the focus (use ControlPaint.DrawFocusRectangle). If this starts to sound unattractive then that's because UC was really meant to be a container control.

Hans Passant
This seemed to do the job. I'll continue to play around with it to sort out the intricacies you mentioned. Thanks!
Sambo
+1 Glad to have another great answer from NoBugz. Curious: in this case do you think the OP might do better using a Form instead of a UserControl ?
BillW
@Bill: the Form class is a container control as well, although it won't fight back quite the same way. My advice would have to be to avoid non-standard UI practices. It confuses the user as well.
Hans Passant
A: 

too long for a comment, includes link, and code ... but this is a comment ...

Lots of people have complained about a UserControl not firing the 'GotFocus() event. For example : UserControl and GotFocus() fyi : LostFocus() will fire as expected, in my experience. In the past, in a multiple Forms project, I've experimented with implementing 'Enter and 'Leave event handlers on a UserControl on each Form, and found that 'Enter is only called on Form load, once.

Evidently the Controls on the UserControl "take focus" (in a way I can't explain, but perhaps one of SO's WinForms gurus will). Perhaps this is related to the fact that UserControl descends from ContainerControl ?

I experimented with writing one 'GotFocus() handler :

    private void Control_GotFocus(object sender, EventArgs e)
    {
        Console.WriteLine("Control GotFocus : " + ((sender as Control).Name));
    }

And then, in the UserControl 'Load event, wired up all the Controls on the UserControl to that event handler : what I observed was that the Control on the UserControl with the lowest TabIndex would fire the 'GotFocus event just on launching the app, and on switching between Forms.

The only other thing I've seen mentioned in this situation is to make sure the 'IsTabStop property of the UserControl is set to 'True : this was from Shawn Wildermuth at MS in the context of a SilverLight related question, so no idea if this might apply in your case.

Another suggestion, which was to write a MouseDown or MouseClick event handler for the UserControl, and in that call: this.SetFocus(); led me nowhere.

Hope you get an answer !

BillW