CHAPTER 19 (Web site construction) BUILDING A BETTER WINDOW WITH
CHAPTER 19 BUILDING A BETTER WINDOW WITH SYSTEM.WINDOWS.FORMS 617 MessageBox.Show(”Right click!”); if (e.Button == MouseButtons.Middle) MessageBox.Show(”Middle click!”); } Responding to Keyboard Events Processing keyboard input is almost identical to responding to mouse activity. The KeyUp and KeyDown events work in conjunction with the KeyEventHandler delegate, which can point to any method taking an object as the first parameter and KeyEventArgs as the second: void MyKeyboardHandler(object sender, KeyEventArgs e); KeyEventArgs has the members of interest shown in Table 19-7. Table 19-7. Properties of the KeyEventArgs Type Property Meaning in Life Alt Gets a value indicating whether the Alt key was pressed Control Gets a value indicating whether the Ctrl key was pressed Handled Gets or sets a value indicating whether the event was fully handled in your handler KeyCode Gets the keyboard code for a KeyDown or KeyUp event Modifiers Indicates which modifier keys (Ctrl, Shift, and/or Alt) were pressed Shift Gets a value indicating whether the Shift key was pressed Update your MainForm to handle the KeyUp event. Once you do, display the name of the key that was pressed inside amessage box using the KeyCode property. public class MainForm : Form { public MainForm() { … // Listen for the KeyUp Event. KeyUp += new KeyEventHandler(MainForm_KeyUp); } private void MainForm_KeyUp (object sender, KeyEventArgs e) { MessageBox.Show(e.KeyCode.ToString(), “Key Pressed!”); } } Now compile and run your program. You should be able to determine not only which mouse button was clicked, but also which keyboard key was pressed. That wraps up our look at the core functionality of the Control base class. Next up, let s check out the role of Form. Source Code The ControlBehaviors project is included under the Chapter 19 subdirectory.
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.