Click here to Skip to main content
15,878,852 members
Articles / Programming Languages / Visual Basic
Tip/Trick

The Cursor.Clip Property

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
29 Apr 2012CPOL 19.2K   2
Freezing the Windows Cursor is done in an easy "one-liner"

Introduction

Sometimes you may want to set the Cursor's bounds, e.g. to forbid the user leaving the form with the cursor.

How To Do

In the Cursor class, there's a property named Clip that enables you to set a Rectangle in which the user can move the cursor.

VB.NET

Forbid to leave form:

VB.NET
Cursor.Clip = Me.Bounds

Make moving possible again:

VB.NET
Cursor.Clip = Rectangle.Empty

C#

Forbid to leave form:

C#
Cursor.Clip = this.Bounds;

Make moving possible again:

C#
Cursor.Clip = Rectangle.Empty;

Points of Interest

This property will be reset if the form loses focus. So If you really want to prohibit the user to move the cursor out of the form, you'll have to set the bounds every time in the Form.MouseEnter event.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
SuggestionPlease explain the motivation for doing this. Pin
John Brett29-Apr-12 23:21
John Brett29-Apr-12 23:21 
GeneralRe: Please explain the motivation for doing this. Pin
User 759270630-Apr-12 2:38
User 759270630-Apr-12 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.