Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The text box is one of the controls available for gathering text input in Silverlight. It has a few interesting events. Let's see what they are. I am going to look at these events in the DetailingKeyEvents project. I'm in this page called MainPage.xaml which has three or four text boxes on the user interface. I am going to start by looking at this first text box. It has the TextChanged event. The TextChanged event fires every time a character in the text box is changed. I am going to show you what I mean.
I'll go to my code behind. Down here in the TextBox_TextChanged event handler, I'm adding a string to the ListBox every time this event fires. When I run the application by pressing F5, and I type a character in here, you see that the TextChanged event fired. If I type another character in, again, it fires. If I copy these two characters, Ctrl+C and delete the item, again, the TextChanged fires.
Now, I am going to paste those two characters in with the Ctrl+V and one more time that event fires. Another interesting event is the KeyUP event. I am going to add a KeyUP at the Grid level, there's LayoutRoot. Type-in KeyUP=, now I've already written the code, so in Visual Studio, what I can do is select the code in this drop-down, and then press Tab. I will switch the code behind and we will see what's happening here.
When this event fires, I'm checking to see which keystrokes were fired and I'm checking to see whether, either we're holding down the Ctrl key, or the Shift key. I'm doing that with this if statement here. This says; if the keyboard modifiers ended together with the control modifier key is == to ModifierKeys.Control. In other words, if you press the Ctrl key, then output this string; Ctrl, space, vertical pipe.
If the Key Modifiers were the Shift key, then add Shift. And after that, it's a code. I add a new item to the list box that takes these Modifier values and it also gets the key value that you type. So I am getting two pieces of information now, the key that you typed and whether I was using the Ctrl or the Shift key. Let me show you. F5 to run the application. I will place my cursor in the second text box. Remember that it put the event at the Grid level, so all the text boxes will be firing that same event.
I am going to type-in the A key, and you can see that it says that was the key A, and no modifiers were applied. Now, I am going to hold down the Ctrl key, and tap A, and now you can see that I got Key = A and then Modifiers = Ctrl. One of my favorite techniques with the KeyUP and KeyDown is to block certain letters from being typed in a text box. Watch what happens here. I am going to type the letter A in this text box. You can see that the A was typed.
I am going to type the letter B, and you can see that the key was typed, but the text box does not show the letter B. How did I do that? Choose this last text box. I've registered a KeyDown event. I am going to go to the code-behind, and then we'll look here, TextBox_KeyDown. I'm using this KeyEventArgs to query the key that was typed. So I say if e.Key is equal to this value that's provided by Silverlight, in other words, the letter B, if the key was the letter B, then say e.Handled.
Now, e.Handled means to stop the event bubbling. That means that the event never reaches the text box, and the text box never enters the letter B in the output. There really are only a handful of key events in Silverlight, even so they provide several valuable services. For example, they can help you determine when text is changed, so that you can warn the user that their data should be saved.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97526 Viewers
61 Video lessons · 84763 Viewers
71 Video lessons · 68945 Viewers
56 Video lessons · 101390 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.