How do I set focus to a Usercontrol
Asked Answered
T

5

7

I have 50 UserControls that I add to a flowlayoutPanel dynamically. I need to set focus to a user control but it doesn't work. I have been searching a lot but can't find any example that I understand.

The only example that I find is this Setting Focus to a .NET UserControl...?

I tried to use userCtrl.Focus(); but it didn't work. As I have been reading the usercontrol doesn't like to have focus.

Telescope answered 13/9, 2016 at 13:59 Comment(5)
Do the UserControls contain other controls? If so, you could focus one of those.Dennie
this.ActiveControl = YourControl;Allsopp
Set ActiveControl as others have commented. Additionally, they're your UserControls! If you give them a public FocusOnMe() method and code that method to set focus where you will, then your form could say myUserControl.FocusOnMe().Pelham
UserControl really despises getting the focus and tries as hard as it can to avoid it. Passing it off to a child control whenever it can. It does not have any use for focus, it cannot tell the user that it has the focus and has no use for keyboard input whatsoever. Which begs the question why you are trying to fight it, very unclear from the question. Your other question about it suggests that you actually want the FLP to have the focus. It doesn't like it either. Giving FLP focus requires surgery like this.Belinda
check this out.Slobber
S
6

Addition: Now that I understand more of the Control class, I understand that if you derive from Control you should not subscribe to its events, but use the On.. functions, like OnEnter. I've changed my answer accordingly

To Activate any Control, including a UserControl use Control.Select().

If you do this for a TextBox, you'll see that Select ensures that it gets the input focus.

I guess you want to do something with the selected UserControl (the Control that has the focus), for instance, you want to change its appearance, or select any of the controls on it. To do this, your UserControl class has to subscribe to the events Control.Enter and Control.Leave

I have created a UserControl with a CheckBox that is automatically checked whenever the UserControl is selected (has the input focus):

Addition: If you derive from a Control, don't subscribe to events Enter and Leave. Instead override the functions that raise these events: OnEnter / OnLeave.

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    protected override void OnEnter(EventArgs e)
    {
        this.checkBox1.Checked = true;
        base.OnEnter(e); // this will raise the Enter event
    }

    protected override void OnLeave(EventArgs e)
    {
        this.checkBox1.Checked = false;
        base.OnLeave(e); // this will raise the Leave event
    }
}

I have a form with a button, and an event handler that is called when the button is clicked:

private void OnButton1Clicked(object sender, EventArgs e)
{
    this.userControl1.Select();
}

Now whenever the button is clicked I see that the user control gets the focus because the check box is checked and whenever I click elsewhere the checkbox is unchecked.

Shavonneshaw answered 13/9, 2016 at 14:37 Comment(0)
A
2

You can set focus to a control by using the ActiveControl Property

this.ActiveControl = myUserControl;
Allsopp answered 13/9, 2016 at 14:16 Comment(0)
S
2

Though you did not detail what did you mean it did not work, focusing has many aspects conventionally.

1. Explicit focusing

Calling Focus() method of a control is the same as setting ActiveControl of the container form. If CanFocus returns true (your control and all its parents are visible and enabled), it works; however, you will have no visual feedback, except some indirect hint, eg. the originally focused control (button or textbox) loses the focus.

To visualize the focused state you might want to use some custom paint:

protected override void OnPaintBackground(PaintEventArgs e)
{
    e.Graphics.Clear(Focused ? SystemColors.Highlight : SystemColors.Control);
}

If you derive directly from Control instead of UserControl, override the following two methods to force a repaint on changing the focused state:

protected override void OnGotFocus(EventArgs e)
{
    Invalidate();
    base.OnGotFocus(e);
}

protected override void OnLostFocus(EventArgs e)
{
    base.OnLostFocus(e);
    Invalidate();
}

2. Focusing by the mouse

To receive focus by clicking the control add this line to the constructor:

SetStyle(ControlStyles.Selectable, true);

If you derive directly from Control instead of UserControl, override the OnMouseDown, too:

protected override void OnMouseDown(MouseEventArgs e)
{
    if (!Focused)
        Focus();
    base.OnMouseDown(e);
}

3. Focusing by the keyboard

To receive focus by the TAB key just set the TabStop property to true and adjust the TabOrder property.

Stier answered 13/9, 2016 at 14:56 Comment(0)
S
0

Example to focus on textBox1:

textBox1.Select();
Syncom answered 13/9, 2016 at 16:52 Comment(0)
T
0

you can try tab index of the user control. If you set its tab index to 1 it will be focused once the program start.

Teetotalism answered 13/9, 2016 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.