O'Reilly logo

Programming Visual Basic 2005 by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. Mice and Fonts

In the previous chapter, you created a custom control from scratch, and placed it in a form. In this chapter, you'll have the clock control that you created respond to mouse events and you'll allow the user to set the clock's fonts.

Every control supports several mouse events, including MouseClick, DoubleClick, MouseEnter, MouseHover, MouseLeave, MouseDown, MouseMove, MouseWheel, and MouseUp.

Tip

The base Control class supports two related events: Click and MouseClick. Click will be fired any time the control is clicked (e.g., by tabbing to it and pressing spacebar or by clicking with the mouse) while MouseClick will only fire if the control is clicked with the mouse.

To test these, we'll add the following behavior:

  • When the user clicks in the control, we'll center the clock on that click point.

  • When the user presses the mouse button, we'll change the cursor to a hand, at the click point.

  • When the user drags the clock (with the mouse button down), we'll move the clock, centered on the mouse location.

  • When the user lets up the mouse button, we'll change the cursor back to default and leave the clock at the location of the MouseUp.

  • When the mouse enters our control, we'll set the background color to light blue.

  • When the mouse leaves our control, we'll restore its default background color.

The net effect is that the user can click on a new location to move the clock, or the user can drag the clock to the new location.

Click the Mouse

To get started, you'll need to change ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required