.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Set Focus To A TextBox In Silverlight

Posted By:Diptimaya Patra       Posted Date: March 27, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

In this article we will see how we can set a focus for a TextBox in silverlight 3. In some of the project requirement this is required that the TextBox must be Focused when you load the page.
 

Introduction

In this article we will see how we can set a focus for a TextBox. In some of the project requirement this is required that the TextBox must be Focused when you load the page.

Known Bug in Silverlight 3

You will find Focus method for almost all UI Element in Silverlight. But when you use it directly, it doesn't work. This is because it is a known bug in Silverlight 3.

What we can do is we can have the following approach to achieve it.

Suppose we have the following TextBox as txtName.


Now when you set focus for this TextBox the Caret should blink when you load the page.

You can achieve this by adding the UpdateLayout method before applying the Focus to the TextBox.

public MainPage()
{
  InitializeComponent();
  txtName.UpdateLayout();
  txtName.Focus();
}

Now when you run the Application you will see the TextBox is set Focus.

If you are trying Navigation Framework there also you can use it but in different place. The frame has the event as OnNavigateTo as follows.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  #region Focusing TextBox
  txtName.UpdateLayout();
  txtName.Focus(); 
  #endregion
}

Hope this trick helps.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend