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.
Addition: Now that I understand more of the
Control
class, I understand that if you derive fromControl
you should not subscribe to its events, but use the On.. functions, likeOnEnter
. 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.
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.
您可以使用ActiveControl属性将焦点设置为控件
this.ActiveControl = myUserControl;
关注textBox1的示例:
textBox1.Select();
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.