Introduction:
TextBox control that can be used to display or edit unformatted text. And some times we may need to manipulate the TextBox text to create cut and paste within the app. So we can learn how to create custom ClipBoard functionality from this article
Requirements:
- This sample is targeted for windows phone WinRT 8.1 OS,So make sure you’ve downloaded and installed the Windows Phone 8.1 SDK or later. For more information, see Get the SDK.
- I assumes that you’re going to test your app on the Windows Phone emulator. If you want to test your app on a phone, you have to take some additional steps. For more info, see Register your Windows Phone device for development.
- This post assumes you’re using Microsoft Visual Studio Express 2013 for Windows or Later.
- This sample is targeted for windows phone WinRT 8.1 OS,So make sure you’ve downloaded and installed the Windows Phone 8.1 SDK or later. For more information, see Get the SDK.
- I assumes that you’re going to test your app on the Windows Phone emulator. If you want to test your app on a phone, you have to take some additional steps. For more info, see Register your Windows Phone device for development.
- This post assumes you’re using Microsoft Visual Studio Express 2013 for Windows or Later.
Description:
Step 1:
1. Open Microsoft Visual Studio Express 2013 for Windows (or) later.
2. Create new windows phone WinRT project using the "Blank App" template available under Visual C# -> Store Apps -> Windows Phone Apps. (for example project name : WinrtCut_Paste)
Step 2: And lets add below code in MainPage.xaml, which is having mainly three buttons for performing copy,cut & paste operation on entered text in TextBox.
- <Grid>
- <StackPanel>
- <!--Sample Tile-->
- <TextBlock Text="WinRT Copy & Past Text:" FontSize="25" />
-
- <!--Textbox for entering text-->
- <TextBox Name="TbxEnteredText" Header="Entered Text:" MinHeight="100" TextWrapping="Wrap" AcceptsReturn="True"/>
-
- <!--Three buttons for copy,cut & paste operations-->
- <Button Content="Copy" HorizontalAlignment="Stretch" Click="CopyClick"/>
- <Button Content="Cut" HorizontalAlignment="Stretch" Click="CutClick" />
- <Button Content="Paste" HorizontalAlignment="Stretch" Click="PasteClick"/>
- </StackPanel>
- </Grid>
Press F5 to run the our project and screen could be like below:
Step 3:
1. How to copy text from TextBox?
In generally to select text, we need to double tap on textbox text, In TextBox,we have property like SelectedText can used to get content of the current selection in the text box. So I taken one global string variable name is ClipboardText for storing selected text of TextBox
- private string ClipboardText;
- private async void CopyClick(object sender, RoutedEventArgs e)
- {
- if (TbxEnteredText .SelectedText != "" && ClipboardText != "")
- {
- //Copy selected text to CustomClipboard
- this.ClipboardText = TbxEnteredText .SelectedText;
- }
- else
- {
- MessageDialog messageDialog = new MessageDialog("Please select text for copy operation");
- await messageDialog.ShowAsync();
- }
- }
2. How to cut text from TextBox?
When user can select the text by double tap on it and press the cut operation button, we need to remove the selected text from the textbox.
It means we need to replace the TextBox original text with the text before and after the selection like below:
- private async void CutClick(object sender, RoutedEventArgs e)
- {
- if (TbxEnteredText .SelectedText != "" && ClipboardText != "")
- {
- //Copy selected text to CustomClipboard
- this.ClipboardText = TbxEnteredText .SelectedText;
-
- var removeAt = TbxEnteredText .SelectionStart;
-
- var AfterSelectionLocText = TbxEnteredText .Text.Substring(removeAt + TbxEnteredText .SelectionLength);
-
- var BeforeSelectionLocText = TbxEnteredText .Text.Substring(0, removeAt);
-
- TbxEnteredText .Text = BeforeSelectionLocText + AfterSelectionLocText;
-
- TbxEnteredText .SelectionStart = BeforeSelectionLocText.Length;
- }
- else
- {
- MessageDialog messageDialog = new MessageDialog("Please select text for cut operation");
- await messageDialog.ShowAsync();
- }
- }
In above code, I am using SelectionStart property which is useful to get character index for the beginning of the current selection.
3. How to paste text in TextBox?
Upto now, when user copy the text or cut the text from textbox, it can stored in our global string is 'ClipboardText '. So to perform paste operation, first we need to get position where user place the cursor on textbox and after that we need to place the our ClipboardText on that selection position.
- private async void PasteClick(object sender, RoutedEventArgs e)
- {
- if (ClipboardText != "" && ClipboardText != null)
- {
- var SelectionLocation = TbxEnteredText .SelectionStart;
- var BeforeSelectionLocText = TbxEnteredText .Text.Substring(0, SelectionLocation);
- var AfterSelectionLocText = TbxEnteredText .Text.Substring(SelectionLocation + TbxEnteredText .SelectionLength);
-
- TbxEnteredText .Text = BeforeSelectionLocText +
- this.ClipboardText +
- AfterSelectionLocText;
-
- TbxEnteredText .SelectionStart = SelectionLocation + this.ClipboardText.Length;
- TbxEnteredText .SelectionLength = 0;
- }
- else
- {
- MessageDialog messageDialog = new MessageDialog("No selected text for paste operation");
- await messageDialog.ShowAsync();
- }
- }
Important Notes:
1. In WinRT, there are more properties added for TextBox control compare to Silverlight platfrom (Ex: Header, PlaceholderText..etc)
2. Just reminder is, this sample can also run for windowsphone silverligh platform. Because this sample used very basic coding to perform copy,cut & paste operation within the app.
3. This sample will be work on windows phone 8.1 OS devices or later. and this sample was tested in Emulator,Nokia Lumia 530, Nokia Lumia 735& Microsoft 535.
FeedBack Note:
Please share your thoughts,what you think about this post,Is this post really helpful for you?I always welcome if you drop comments on this post and it would be impressive.
Follow me always at @Subramanyam_B
Have a nice day by Subramanyam Raju :)
- <Grid>
- <StackPanel>
- <!--Sample Tile-->
- <TextBlock Text="WinRT Copy & Past Text:" FontSize="25" />
- <!--Textbox for entering text-->
- <TextBox Name="TbxEnteredText" Header="Entered Text:" MinHeight="100" TextWrapping="Wrap" AcceptsReturn="True"/>
- <!--Three buttons for copy,cut & paste operations-->
- <Button Content="Copy" HorizontalAlignment="Stretch" Click="CopyClick"/>
- <Button Content="Cut" HorizontalAlignment="Stretch" Click="CutClick" />
- <Button Content="Paste" HorizontalAlignment="Stretch" Click="PasteClick"/>
- </StackPanel>
- </Grid>
Step 3:
1. How to copy text from TextBox?
In generally to select text, we need to double tap on textbox text, In TextBox,we have property like SelectedText can used to get content of the current selection in the text box. So I taken one global string variable name is ClipboardText for storing selected text of TextBox
- private string ClipboardText;
- private async void CopyClick(object sender, RoutedEventArgs e)
- {
- if (TbxEnteredText .SelectedText != "" && ClipboardText != "")
- {
- //Copy selected text to CustomClipboard
- this.ClipboardText = TbxEnteredText .SelectedText;
- }
- else
- {
- MessageDialog messageDialog = new MessageDialog("Please select text for copy operation");
- await messageDialog.ShowAsync();
- }
- }
2. How to cut text from TextBox?
When user can select the text by double tap on it and press the cut operation button, we need to remove the selected text from the textbox.It means we need to replace the TextBox original text with the text before and after the selection like below:
- private async void CutClick(object sender, RoutedEventArgs e)
- {
- if (TbxEnteredText .SelectedText != "" && ClipboardText != "")
- {
- //Copy selected text to CustomClipboard
- this.ClipboardText = TbxEnteredText .SelectedText;
- var removeAt = TbxEnteredText .SelectionStart;
- var AfterSelectionLocText = TbxEnteredText .Text.Substring(removeAt + TbxEnteredText .SelectionLength);
- var BeforeSelectionLocText = TbxEnteredText .Text.Substring(0, removeAt);
- TbxEnteredText .Text = BeforeSelectionLocText + AfterSelectionLocText;
- TbxEnteredText .SelectionStart = BeforeSelectionLocText.Length;
- }
- else
- {
- MessageDialog messageDialog = new MessageDialog("Please select text for cut operation");
- await messageDialog.ShowAsync();
- }
- }
3. How to paste text in TextBox?
Upto now, when user copy the text or cut the text from textbox, it can stored in our global string is 'ClipboardText '. So to perform paste operation, first we need to get position where user place the cursor on textbox and after that we need to place the our ClipboardText on that selection position.
- private async void PasteClick(object sender, RoutedEventArgs e)
- {
- if (ClipboardText != "" && ClipboardText != null)
- {
- var SelectionLocation = TbxEnteredText .SelectionStart;
- var BeforeSelectionLocText = TbxEnteredText .Text.Substring(0, SelectionLocation);
- var AfterSelectionLocText = TbxEnteredText .Text.Substring(SelectionLocation + TbxEnteredText .SelectionLength);
- TbxEnteredText .Text = BeforeSelectionLocText +
- this.ClipboardText +
- AfterSelectionLocText;
- TbxEnteredText .SelectionStart = SelectionLocation + this.ClipboardText.Length;
- TbxEnteredText .SelectionLength = 0;
- }
- else
- {
- MessageDialog messageDialog = new MessageDialog("No selected text for paste operation");
- await messageDialog.ShowAsync();
- }
- }
Important Notes:
1. In WinRT, there are more properties added for TextBox control compare to Silverlight platfrom (Ex: Header, PlaceholderText..etc)
2. Just reminder is, this sample can also run for windowsphone silverligh platform. Because this sample used very basic coding to perform copy,cut & paste operation within the app.
3. This sample will be work on windows phone 8.1 OS devices or later. and this sample was tested in Emulator,Nokia Lumia 530, Nokia Lumia 735& Microsoft 535.
FeedBack Note:
Please share your thoughts,what you think about this post,Is this post really helpful for you?I always welcome if you drop comments on this post and it would be impressive.
Follow me always at @Subramanyam_B
Have a nice day by Subramanyam Raju :)
How can we disable the copy/paste/cut options for the textbox.?
ReplyDeleteWindows 8.1 Home, Pro & Enterprise Retail version can be found here: http://www.coreyz.com/Microsoft-Windows-8-1-Retail
ReplyDeleteThanks for sharing informative article. Download Windows 7 ultimate for free from getintopc. It helps you to explore full functionality of windows operating system.
ReplyDeleteThank you !! Very usefull !!
ReplyDeletedot net training in chennai
Dot Net is an ever trending technology where it is more preferable by developers to utilize the features in the dot net language. your article on Dot Net language proves that it is an evergreen technology in the IT market.
ReplyDelete.net training in velachery |
Dot Net Training