This is a migrated thread and some comments may be shown as answers.

Textbox focus inside window not working

2 Answers 92 Views
Window
This is a migrated thread and some comments may be shown as answers.
Trude
Top achievements
Rank 2
Trude asked on 17 Nov 2009, 11:09 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Miroslav Nedyalkov
Telerik team
answered on 17 Nov 2009, 01:49 PM
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.
0
Trude
Top achievements
Rank 2
answered on 17 Nov 2009, 05:57 PM
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  
 
Tags
Window
Asked by
Trude
Top achievements
Rank 2
Answers by
Miroslav Nedyalkov
Telerik team
Trude
Top achievements
Rank 2
Share this question
or