How to Snap the Cursor to a Button!
If you're attempting to create that foolproof Windows application, one great technique to use is that of snapping the cursor to a particular control, anticipating the next click.
The following neat little function does exactly that. Simply pass in a control to get it started: It'll calculate the exact bottom middle location of the control, then snap the cursor to that position. Here's the code:
Public Sub SnapToControl(ByVal Control As Control)
' Snaps the cursor to the bottom middle of the passed control
Dim objPoint As Point = Control.PointToScreen(New Point(0, 0))
objPoint.X += (Control.Width / 2)
objPoint.Y += ((Control.Height / 4) * 3)
Cursor.Position = objPoint
End Sub
And here's how you might use this to snap to, say, a Button control:
SnapToControl(Button1)

Figure: A simple little application, this time snapping to a LinkLabel control
About the Author
Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2, $49.99), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.
More for Developers
On the Codeguru Forums
Visit the Forums »Featured Partner Resources
Get your Android Apps ready for Intel® Atom™ processor-based smartphones and tablets now.
Use the Android NDK to deliver the best performance on Intel® Atom™ processor-based devices.
The Android community on the Intel® Software Network has everything you need to prepare your apps for Intel® Atom™ processor-based devices.



Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.