Text Selection In TextBox In Silverlight 3 Application

No.of Views682
Bookmarked0 times
Downloads 
Votes0
By  dpatra   On  06 Apr 2010 09:04:52
Tag : Silver Light and XAML , Miscellaneous Controls
In this article we will see some of the properties related to Text Selection in TextBox Control in Silverlight 3.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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

Creating Silverlight Project

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

 

Image Loading

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:

 

Image Loading

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:

<TextBox x:Name="MyTextBox" Margin="8" Text="TextBox" TextWrapping="Wrap" SelectionChanged="MyTextBox_SelectionChanged"/>


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.

 

Image Loading

Conclusion

Select the text from the textblock in silver light.enjoy coding.

 
Sign Up to vote for this article
 
About Author
 
dpatra
Occupation-Not Provided
Company-Not Provided
Member Type-Expert
Location-Not Provided
Joined date-13 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
By:RRaveenDate Of Posted:4/6/2010 9:25:58 PM
Great work
This is excellent article.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top