Set focus to TextBox

43,540

Solution 1

This seems to be a Silverlight bug. I've used the TextChanged event on the TextBox, and set the Focus() in there. Kind of an ugly workaround, but it worked for me. If anybody has another solution, please post.

Solution 2

Does this help?

this.ActiveControl = textBox1;

Solution 3

If you want the text box to be selected without you having to click into it (if that is what you mean by focus), try:

textBox1.Select();

Solution 4

Even I have tried all the above solutions but didn't work for me. Finally I tried with the following thing, and it worked.

private void txtBox_LayoutUpdated(object sender, EventArgs e)
    {
        txtBox.Focus();
    }

Solution 5

Just set focus property of any control at Form_Activated time..

private void Form_Activated(Object sender, EventArgs e){
 Textbox.Focus();
}
Share:
43,540
Adrian Marinica
Author by

Adrian Marinica

while(true) { continue; }

Updated on July 17, 2020

Comments

  • Adrian Marinica
    Adrian Marinica almost 4 years

    In my WindowsPhone application I have a Text Box that needs to receive focus at a given time. What I've tried so far:

    textBox1.Focus();
    

    textBox1.UpdateLayout();
    textBox1.Focus();
    

    textBox1.IsTabStop = true;
    textBox1.UpdateLayout();
    textBox1.Focus();
    

    textBox1.IsTabStop = true;
    textBox1.Focus();
    

    Nothing seems to work. In the Emulator, when the Focus() method is called, the keyboard starts to rise, but then crashes back. The TextBox has IsTabStop set to true in the properties.

  • Adrian Marinica
    Adrian Marinica over 8 years
    As a note, my issue was caused by a bug that was occurring in 2011. After 4 years, it might have been fixed, so this issue might not reproduce anymore.
  • Adrian Marinica
    Adrian Marinica over 7 years
    I can't believe 5 years ago I was writing Windows Phone applications and now this OS is approaching its final days.
  • Prince
    Prince over 7 years
    Indeed I actually enjoy writing apps on this OS it's way easier than developing on android native. I guess most devs don't want to port their applications to the platform... I'll continue to develop for the platform till it's final day.
  • Hassan Rahman
    Hassan Rahman over 6 years
    This solution works in WPF not in UWP / Windows Phone.