SIGN UP MEMBER LOGIN:    
ARTICLE

Setting Focus of TextBox in Silverlight 3

Posted by Diptimaya Patra Articles | Silverlight with C# October 13, 2009
In this article we will see how we can set a focus for a TextBox.
Reader Level:


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.

<
TextBox x:Name="txtName" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap" Margin="10,40,0,0"/>

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.

Login to add your contents and source code to this article
share this article :
post comment
 

This code is not working at all.You can try with this.

Posted by Raju Ghosh Feb 21, 2011

This code is not working at all.

Posted by Raju Ghosh Feb 21, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor