Sams Teach Yourself C# in 24 Hours
- Table of Contents
- Copyright
- About the Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Audience and Organization
- Conventions Used in This Book
- Onward and Upward!
- Part I. The Visual Studio Environment
- Hour 1. A C# Programming Tour
- Hour 2. Navigating C#
- Hour 3. Understanding Objects and Collections
- Hour 4. Understanding Events
- Part II. Building a User Interface
- Hour 5. Building FormsPart I
- Hour 6. Building FormsPart II
- Hour 7. Working with the Traditional Controls
- Hour 8. Advanced Controls
- Hour 9. Adding Menus and Toolbars to Forms
- Hour 10. Drawing and Printing
- Part III. Making Things HappenProgramming!
- Hour 11. Creating and Calling Methods
- Hour 12. Using Constants, Data Types, Variables, and Arrays
- Hour 13. Performing Arithmetic, String Manipulation, and Date/Time Adjustments
- Hour 14. Making Decisions in C# Code
- Hour 15. Looping for Efficiency
- Hour 16. Debugging Your Code
- Hour 17. Designing Objects Using Classes
- Hour 18. Interacting with Users
- Part IV. Working with Data
- Hour 19. Performing File Operations
- Hour 20. Controlling Other Applications Using Automation
- Hour 21. Working with a Database
- Part V. Deploying Solutions and Beyond
- Hour 22. Deploying a Solution
- Hour 23. Introduction to Web Development
- Hour 24. The 10,000-Foot View
- Appendix A. Answers to Quizzes/Exercises
Interacting with the Keyboard
Most every control handles its own keyboard input; however, on occasion, you'll want to handle keyboard input directly. For example, you might want to perform an action when the user presses a specific button or releases a specific button. Most controls support three events that you can use to work directly with keyboard input. These are listed in Table 18.4.
Table 18.4. Events That Handle Keyboard Input
| Event Name | Description |
| KeyDown | Occurs when a key is pressed down while the control has the focus. |
| KeyPress | Occurs when a key is pressed (the key has been pushed down and then released) while the control has the focus. |
| KeyUp | Occurs when a key is released while the control has the focus. |
These events fire in the same order in which they appear in Table 18.4. Suppose, for example, that the user presses a key while a text box has the focus. The following list shows how the events would fire for the text box:
- When the user presses a key, the KeyDown event fires.
- When the user releases a key, the KeyPress event fires.
- After the KeyPress event fires, the KeyUp event fires, completing the cycle of keystroke events.
You're now going to create a project that illustrates handling keystrokes. This project has a text box that will refuse to display the letter "k." Start by creating a new Windows Application titled Keyboard Example. Change the name of the default form to fclsKeyboardExample, set its Text property to Keyboard Example, and set the Main() entry point of the project to reference fclsKeyboardExample instead of Form1. Add a new text box to the form and set its properties as shown in the following table:
| Property | Value |
| Name | txtInput |
| Location | 24,80 |
| Multiline | true |
| Size | 240,120 |
| Text | (make blank) |
You're going to add code to the KeyPress event of the text box to "eat" keystrokes made with the letter "k." Select the text box on the form and open the events list in the Properties window (Remember, this is the lightning bolt icon). Scroll through the list and locate the KeyPress event. Next, double-click the KeyPress event to access it in code. Your code editor should look now look like Figure 18.8.
Figure 18.8 The KeyPress event is a good place to handle keyboard entry.
As you learned in Hour 4, "Understanding Events," the e parameter contains information about the occurrence of this event. In the keyboard events, the e parameter contains information about the key being pressed; therefore, it's what you'll be using to work with the keystroke made by the user.
The key being pressed is available as the KeyChar property of the e parameter. You are going to write code that handles the keystroke when the key being pressed is "k." Add the following code to the KeyPress event:
if (e.KeyChar == 'k')
e.Handled = true;
I would imagine that you're curious about the Handled property of the e object. When you set this property to true, you are telling C# that you handled the keystroke, and C# should ignore it. To see the effect this has, press F5 to run the project and enter the following into the text box:
Heard any good jokes lately?
What you'll end up with is the text shown in Figure 18.9.
Figure 18.9 Notice how the letter k was "eaten" by your code.
Go ahead, try to enter another "k"—you can't. Next, try to enter an uppercase "K"; C# allows you to do this because uppercase and lowercase characters are considered different characters. Want to catch all "Ks," regardless of case? You could do so by adding the OR (||) operand to your decision construct, like this:
if (e.KeyChar == 'k'|| e.KeyChar == 'K')
e.Handled = true;
Using the Common Mouse Events | Next Section

Account Sign In
View your cart