Set focus on TextBox in UserControl - c#

In my program I have a user control that displays data on a window using a content presenter. I would like to simply set the cursor focus on a certain textBox in my window at startup.
Usually I would do this through the code-behind of the window, like this: textBox.Focus();
However, the textBox is defined in the user control, and doesn't seem to work the same way. So far I have tried the same method as above in the user control's code-behind.
Why doesn't this work? How do I set the focus if the textBox is defined in a user control?
What I have tried....:
User Control:
public UserControl()
{
InitializeComponent();
FocusManager.SetFocusedElement(this, textBox);
}
User Control:
public UserControl()
{
InitializeComponent();
textBox.Focusable = true;
Keyboard.Focus(textBox);
}

Give this a try: FocusManager.SetFocusedElement
FocusManager.SetFocusedElement(parentElement, textBox)
or from the msdn website:
textBox.Focusable = true;
Keyboard.Focus(textBox);
Note: You can't set focus in a constructor. If you are, UI Elements have not been created at that point. You should set focus during the Loaded event of your control.

A little bit late but what it really worked for my was
public UserControl()
{
InitializeComponent();
Dispatcher.BeginInvoke(new System.Action(() => { Keyboard.Focus(TextBox); }),
System.Windows.Threading.DispatcherPriority.Loaded);
}

You can try setting the focus in the Loaded or Initialized event of the User control. Eg:
private void MyWpfControl_Load(object sender, EventArgs e)
{
textBox.Focusable = true;
Keyboard.Focus(textBox);
}
Info: Loaded event or Initialized event

Related

How to make correct initialization of ComboBox?

I need show droppeddown combobox after start program.
I need in dropdown style only, not simple style.
This is simple fragment of my program:
private void Form1_Shown(object sender, EventArgs e)
{
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
}
But I found the watch sign as cursor till I click on Form in any place.
I guessed that my Form have not fully active state and wait for something.
When I click Form (or combobox or any control) by LBM, it activated fully and all works fine.
Of course the combobox is dropup then, so I need click combobox twice.
Еell me please what is correct initialization of such style combobox without "Cursor = Cursors.Default;"
You can simply wait until cursor is the default:
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
Application.DoEvents simply process messages from the window queue, so you can process message until you get that cursor is the default. In that moment, you can drop down your control without problem.
If you prefer, create a extension method for the Form:
public static class FormExtends
{
public static void WaitToDefaultCursor(this Form form)
{
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
}
}
And use it:
this.WaitToDefaultCursor();
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
NOTE: I use Cursor.Default but not to change the cursor. The form is processing messages and it's difficult to select a good moment to drop down the control.

Is there a way to disable buttons on main form via a user control

I am trying to disable some buttons on my main form using a user control that is in a panel on main window. This code doesn't work I think because of inheritance.
MainWindow mW = new MainWindow();
mW.button1.Enabled = false;
mW.button2.Enabled = false;
mW.button3.Enabled = false;
mW.button4.Enabled = false;
mW.button5.Enabled = false;
mW.button6.Enabled = false;
mW.button7.Enabled = false;
Please advice some other method.
You could use this.FindForm() to get a reference to the form containing the user control, cast that reference back to MainForm, and control the buttons on your MainForm (assuming they are internal or public).
However, that would probably be the wrong thing to do.
A better approach would be to create an event in your user control, have the main form listed to that event, and in the event handler change whatever you want in the main form.
This way, your user control is not bound to a specific form, and you can reuse it in other forms as well.
You can inherit the EventArgs class to send specific data with the event - in this case, a boolean value indicating if the buttons should be enabled or disabled:
public class MyEventArgs : EventArgs
{
public MyEventArgs(bool enable)
{
Enable = enable;
}
public bool Enable {get;}
}
And then, in your user control:
public event EventHandler<MyEventArgs> SomethingHappened;
// to raise it:
SomethingHappend?.Invoke();
In the main form, you subscribe to the event:
MyUserControl.SomethingHappened += MyUserControl_SomethingHappened;
And in the event handler you enable / disable the buttons:
private void MyUserControl_SomethingHappened(object sender, MyEventArgs e)
{
button1.Enabled = e.Enable;
button2.Enabled = e.Enable;
...
button7.Enabled = e.Enable;
}

How do I set focus to a Usercontrol

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 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.
You can set focus to a control by using the ActiveControl Property
this.ActiveControl = myUserControl;
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.
Example to focus on 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.

How to prevent user control from stealing focus?

I created a user control that functions as a keyboard. I arranged the buttons in the control in the exact same manner as a keyboard, and set (ControlStyles.Selectable, false). This allows the focus to stay in a textbox that the user selected on the parent form. I also set the style of the user control to not be selectable. Everything works great when the user presses the buttons, but when the user control gets clicked, it steals the focus away from the textbox. Here is my code:
public partial class TouchKeyboard : UserControl
{
public TouchKeyboard()
{
InitializeComponent();
SetStyle(ControlStyles.Selectable, false);
}
private void TouchKeyboard_Load(object sender, EventArgs e)
{
foreach (var button in this.Controls.OfType<CustomControls.CustomButton>())
{
button.Click += new EventHandler(this.ButtonClick);
}
}
private void ButtonClick(object sender, EventArgs e)
{
var button = sender as CustomControls.CustomButton;
if (button.Name == "btnSpace")
{
SendKeys.Send(" ");
}
else
{
SendKeys.Send("{" + button.Text + "}");
}
}
}
Why would my user control steal focus when ControlStyles.Selectable is set to false?
After a few days of screwing with it, I finally figured it out. Since panels don't steal focus, I docked a panel in the User Control and placed my buttons inside it. That fixed the problem.

Programatically set Focus to a TextBox in Windows Phone

In a Windows Phone app I have an TextBox and a Button. The user writes some text to the TextBox and taps the Button, the text from the TextBox is added to a list. The TextBox loses focus after the Button is tapped.
What I want to do is to set the focus back to the TextBox after the Button is tapped so the user can continue writing another text without needing to tap the TextBox.
I tried calling the Focus() method of the TextBox in the Button handler but this does not work. is there another, if any, way to do this?
When Button clicked try to add bollean flag = true. Then check this flag on event OnTextBoxLostFocus.
<TextBox x:Name="tb" Grid.Row="1" LostFocus="Tb_OnLostFocus"/>
<Button x:Name="btn" Click="Btn_OnClick" />
public partial class MainPage : PhoneApplicationPage
{
private bool flag;
public MainPage()
{
InitializeComponent();
}
private void Btn_OnClick(object sender, RoutedEventArgs e)
{
flag = true;
tb.Focus();
}
private void Tb_OnLostFocus(object sender, RoutedEventArgs e)
{
if (!flag) return;
tb.Focus();
flag = false;
}
}
Hope its help.
I have tried a lot of solutions, but this is the only one that works for me (Windows Phone 8.1 app).
First catch your TextBox's Loaded event, then call Focus(FocusState.Keyboard).
private void myTextBox_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Focus(FocusState.Keyboard);
}
Even I tried with lots of above solutions but none of them worked for me as am trying to focus on page load. Finally I got this solution and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}
What happens if you call:
yourTextBox.Select(0,0)
http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.select.aspx
you can accomplish this by programmatically giving it focus. This can be done by
calling its Focusmethod, although this call can fail (and return false) under certain conditions.
For example, you cannot set focus on a control from a page’s constructor; it’s too early. You can,
however, call it from a page’s Loadedevent.
The way that it worked best for me on the phone was, if I wanted to focus on a particular textbox when the page loaded:
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());
}
or if I just wanted it at a certain point. Just repeat these two lines:
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());

Categories