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

Top 5 Contributors of the Month
Melody Anderson

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

 Subscribe to Articles

Select text from TextBox In Silverlight Application

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

Select text from TextBox In Silverlight Application.In this article we will see some of the properties related to Text Selection in Text Box Control in Silverlight3.


In this article we will see some of the properties related to Text Selection in TextBox Control in Silverlight 3.

Crating Silverlight Project

Fire up Visual Studio 2008 and create a Silverlight Application. Name it as TextSelectionInSL3.


As you already know, you can select text in any text box by clicking and dragging with the mouse or holding down Shift while you move through the text with the arrow keys. The TextBox class also gives you the ability to determine or change the currently selected text programmatically, using the SelectionStart, SelectionLength, and SelectedText properties.

Open the solution in Expression Blend 3 and design your application.

I have designed in the following fashion:


As you see from the above figure, I have a TextBox, and three TextBlocks to display the Selection Properties.
SelectionStart identifies the zero-based position where the selection begins. For example, if you set this property to 10, the first selected character is the 11th character in the text box. The Selection Length indicates the total number of selected characters. (A value of 0 indicates no selected characters.) Finally, the SelectedText property allows you to quickly examine or change the selected text in the text box.

Go to Visual Studio and add an event for Selection Changed for the TextBox, follow the code below:

Now add the following code to see how the properties are used.

private void  MyTextBox_SelectionChanged(object sender, RoutedEventArgs e)
  TextPosition.Text = String.Format("Selected Position : From {0} To {1}",MyTextBox.SelectionStart, (MyTextBox.SelectionLength+MyTextBox.SelectionStart)-1);
  TextLength.Text = String.Format("Selected Length : {0}", MyTextBox.SelectionLength);
  TextSelected.Text = String.Format("Selected Text : \"{0}\"",MyTextBox.SelectedText);

Now run your application and Type your text and select a particular length. You will see the TextBlocks texts are changing displaying the Selection Position, Selection Length and Selected Text.


Enjoy Coding.
 Subscribe to Articles


Further Readings:


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