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
Using the Common Mouse Events
As with keyboard input, most controls support mouse input natively; you don't have to write code to deal with mouse input. However, at times you need more control than that offered by the native functionality of a control. C# supports six events that enable you to deal with mouse input directly. These events are listed in Table 18.5, in the order in which they occur.
Table 18.5. Events Used to Handle Mouse Input
| Event Name | Description |
| MouseEnter | Occurs when the pointer enters a control. |
| MouseMove | Occurs when the pointer moves over a control. |
| MouseHover | Occurs when the pointer hovers over a control. |
| MouseDown | Occurs when the pointer is over a control and a button is pressed. |
| MouseUp | Occurs when the pointer is over a control and a button is released. |
| MouseLeave | Occurs when the pointer leaves a control. |
You're now going to build a project that illustrates interacting with the mouse, using the MouseMove event. This project will allow a user to draw on a form, much like you can draw in a paint program. Begin by creating a new Windows Application titled Mouse Paint. Change the name of the default form to fclsMousePaint, set its Text property to Paint with the Mouse, and set the Main() entry point of the project to reference fclsMousePaint instead of Form1.
Next, double-click the form to access its default event, the Load event. Enter the following statement into the load event:
m_objGraphics = this.CreateGraphics();
You've already used a graphics object a few times. What you're doing here is setting a graphics object to the client area of the form; any drawing performed on the object will appear on the form. Because you're going to draw to this graphics object each time the mouse moves over the form, there's no point in creating a new graphics object each time you need to draw to it. Therefore, you're going to make m_objGraphics a module-level variable, which will be instantiated only once—in the Load event of the form. Enter this statement below the opening curly-brace after the public class fclsMousePaint class declaration:
private Graphics m_objGraphics;
As I've said previously, you should always destroy objects when you're done with them. In this case, you want the object to remain in existence for the life of the form. Therefore, you'll destroy it in the Closed event of the form, which occurs when the form is unloaded. Return to the Form1.cs[Design] tab, open the events list in the Property window and double-click the Closed event to create and open the code window to the Closed event. Enter the following statement in the Closed event:
m_objGraphics.Dispose();
Your form should now look like the one shown in Figure 18.10.
Figure 18.10 Code in many places often works together to achieve one goal.
The last bit of code you need to add is the code that will draw on the form. You're going to place code in the MouseMove event of the form to do this. First, the code will make sure the left mouse button is held down. If it's not, no drawing will take place; the user must hold down the mouse button to draw. Next, a rectangle will be created. The coordinates of the mouse pointer will be used to create a very small rectangle that will be passed to the DrawEllipse method of the graphics object. This has the effect of drawing a tiny circle right where the mouse pointer is positioned. Again, Return to the Form1.cs[Design] tab, open the events list in the Property window, and double-click the MouseMove event to create and open the code window to the MouseMove event. Add the following code to this event:
Rectangle rectEllipse = new Rectangle() ; if (e.Button != MouseButtons.Left) return; rectEllipse.X = e.X - 1; rectEllipse.Y = e.Y - 1; rectEllipse.Width = 2; rectEllipse.Height = 2; m_objGraphics.DrawEllipse(System.Drawing.Pens.Blue, rectEllipse);
Like all events, the e object contains information related to the event. In this example, you are using the X and Y properties of the e object, which is the coordinate of the pointer when the event fires. In addition, you're checking the Button property of the object to make sure the user is pressing the left button.
Your project is now complete! Save your work by clicking Save All on the toolbar, and then press F5 to run the project. Move your mouse over the form—nothing happens. Now, hold down the left mouse button and move the mouse. This time, you'll be drawing on the form (see Figure 18.11).
Figure 18.11 Capturing mouse events opens many exciting possibilities.
Notice that the faster you move the mouse, the more space appears between circles. This shows you that the user is able to move the mouse faster than the MouseMove event can fire, so you can't get every single movement of the mouse. This is important to remember.
Summary | Next Section

Account Sign In
View your cart