Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Text input is an essential part of most business applications. Silverlight has several controls for text input. I will look at the TextBox and password control in this movie. The rich TextBox, auto complete box, and other controls are detailed elsewhere in this chapter. The data grid and data form are showcased in the data chapter. I'm using Visual Studio for this demonstration and I have opened a project called GatheringTextInput. This has one MainPage, which has a button for launching an Options dialog. It's common in applications to show an options dialog to solicit user information.
My dialog is created using a Silverlight class called a child window. I will run the application by pressing F5 and give you a short tour. Here is my main UI. I am going to click on this button to launch the child window. A child window is similar to a modal dialog on other frameworks. It floats over the Main Silverlight UI and must be dismissed by the user to return to the main application interface. As you can see, if I try to click on the main UI, I can't. I must click on OK or Cancel to return.
Now I will reopen it. I am using this dialog to demonstrate some of the TextBox features. The first TextBox is simple. It is a single line control that allows text input. If the text is too long to fit on the TextBox, it is clipped on the right edge of the control. Copy some text and type some more text in, and as you can see, I'll press the Home key now, it's being clipped on the right edge. The next control is a Password box. It conceals what is typed. As you can see my input is masked with small black dots.
You can replace the black dots with your own characters by editing the PasswordChar property. Let me show you an example. I'll switch back to Visual Studio, open my OptionsDialog, scroll to the PasswordBox section, and you can see that the PasswordChar is set to that Unicode character. And I'll return back to the browser and my child window. The next TextBox is a Multiline control. As the text gets longer, it wraps to the next line. This is accomplished by setting the text wrapping property.
I'll use this Multiline TextBox to demonstrate text selection. That of course means selecting the text within the TextBox. Note the four buttons, each one reveals a facet of text selection. This first button will get the selected text and add it to this TextBox on the top. So I will select this first word and then I'll click on Get Selected. Now I'll select the last word and click Get Selected. I can set the selected value by clicking here.
This tells it where to start the selection and how many characters to select. It also has a built-in Select All function and it has the ability to take your current selection and replace it with another string. Let me show you how that code is implemented. I am in the OptionsDialog C# file. Here was the first demo, getting the selected text. That's easy, I just use the SelectedText property. You take the selected text, stored it in a string, and then assign that string to another TextBox.Text property.
To select some of the characters I use the Select method. Here I tell it the starting point and the length of chars that I want. I'm starting at the 12th char and selecting 30 characters. It's easy to select all the text, just call the SelectAll method. And then my last demonstration was where I replaced the selection with my own text. Here I have an if statement that checks to make sure I do have something selected, that the SelectionLength is greater than 0. If that is true, then I take this string and use it to replace the current SelectedText.
Next, I'd like to talk about the clipboard. We take copy and paste for granted. We expect it to work everywhere. Silverlight has limited support for copy and paste however. It supports copying Unicode text only. Images and other rich content are not permitted. The good news is that the TextBox has native support for text, copy, and paste. I will run the application again, click on the Options, open up Notepad where I have some text typed in here. I will copy this text, move over to my Silverlight application and paste it into this TextBox.
So the TextBox as I said has native support for copy and paste. You can also programmatically access the text in the clipboard. I can paste from the clipboard, delete this text and click on this button. This is a security risk, so Microsoft Silverlight asks the user, do you want to allow this Web application to access your clipboard? I will say Yes, and now you can see the text was placed here. And I can Add text to the clipboard, click on this button, the text was added to the clipboard.
And I'll come over to Notepad and do a Ctrl+V to paste it in. Let me show you the code that makes this happen. It's quite simple. Here is the code for getting the clipboard text, I go to the Clipboard object and call its GetText method. I also check ahead of time, make sure that there's something I can use in Silverlight. I call the ContainsText method to see if it has Unicode text in it. To set the text in the clipboard, I just call Clipboard.SetText. TextBox is restricted to a single typeface for all the characters.
Watch the rich text movie to learn how to handle multiple typefaces within a single control.
Get unlimited access to all courses for just $25/month.Become a member