Textbox focus inside window not working

3 posts, 1 answers
  1. Jorn
    Jorn avatar
    222 posts
    Member since:
    Jun 2009

    Posted 17 Nov 2009 Link to this post

    When I open a window (in code behind with show or showdialog) I want to set focus on a specific textbox, but it doesn't seem to work.

    Below is the code:

     

     

    Private Sub login_window_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded  
     
    TextBoxUsername.Focus()  
     
    End Sub  
     

     

  2. Answer
    Miroslav Nedyalkov
    Admin
    Miroslav Nedyalkov avatar
    1718 posts

    Posted 17 Nov 2009 Link to this post

    Hello Jorn,

    What I would recommend you is not to focus an element explicitly, but to put it in the first place in Xaml in the Window's content or to use the TabIndex properties of the visual elements. As I noticed that your window is called LoginWindow I guess it is opened at the start of the application. Please, notice that the Silverlight plugin must be focused in order your TextBox to be focused.

    The work-around if you still want to do that with code is to focus the element asynchronously with a Dispatcher.


    Sincerely yours,
    Miroslav Nedyalkov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. Jorn
    Jorn avatar
    222 posts
    Member since:
    Jun 2009

    Posted 17 Nov 2009 Link to this post

    After going through all your suggestions it turned out that the silverlight control/plugin didn't have focus set. I somehow assumed it automatically gained focus upon loading the application, but this is obviously not the case.

    The following codebehind in the usercontrol solved the problem:

     

    Public Sub New()  
     
    InitializeComponent()  
     
    System.Windows.Browser.HtmlPage.Plugin.Focus()  
     
    End Sub  
     
Back to Top