Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6074113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:20:45+00:00 2026-05-23T10:20:45+00:00

Can someone give me a sample code to confine the cursor on to a

  • 0

Can someone give me a sample code to confine the cursor on to a Form. I found this (ClipCursor API, that says it can be done using it). I have a C# Windows form application and using VS 2008.

My code:

using System;
 using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }


    private void Form1_CursorChanged(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
            MoveCursor ();

    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        MoveCursor();
    }

    private void Form1_LocationChanged(object sender, EventArgs e)
    {
        MoveCursor();
    }

    private void MoveCursor()
    {
        Cursor.Clip = Bounds;
       this.Capture = true;
    }
  }
 }

Form1.Designer.cs

namespace WindowsFormsApplication1
 {
    partial class Form1
   {
       /// <summary>
       /// Required designer variable.
      /// </summary>
       private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.AutoCompleteCustomSource.AddRange(new string[] {
        "all",
        "allah",
        "allo"});
        this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
        this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
        this.textBox1.Location = new System.Drawing.Point(30, 70);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(227, 20);
        this.textBox1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 264);
        this.Controls.Add(this.textBox1);
        this.Cursor = System.Windows.Forms.Cursors.No;
        this.Name = "Form1";
        this.Text = "Form1";
        this.CursorChanged += new System.EventHandler(this.Form1_CursorChanged);
        this.Load += new System.EventHandler(this.Form1_Load);
        this.Resize += new System.EventHandler(this.Form1_Resize);
        this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
   }
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:20:45+00:00Added an answer on May 23, 2026 at 10:20 am

    There’s an option that doesn’t require you to p/invoke: Cursor.Clip

    Edit: new code. Complete form code in single file.

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1 {
      public partial class Form1 : Form {
         public Form1()     {
             InitializeComponent();
          }
          private void Form1_Activate(object sender, EventArgs e)     {
              MoveCursor ();
          }
          private void Form1_Resize(object sender, EventArgs e)     {
             MoveCursor();
         }
          private void Form1_LocationChanged(object sender, EventArgs e)     {
             MoveCursor();
         }
          private void MoveCursor()
          {
             this.Capture = true;
             System.Windows.Forms.Cursor.Clip = Bounds;
         }
    
        private void InitializeComponent()
        {
             this.textBox1 = new System.Windows.Forms.TextBox();
             this.SuspendLayout();
             this.textBox1.AutoCompleteCustomSource.AddRange(new string[] {"all", "allak", "allo"});
             this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
             this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
             this.textBox1.Location = new System.Drawing.Point(30, 70);
             this.textBox1.Name = "textBox1";
             this.textBox1.Size = new System.Drawing.Size(227, 20);
             this.textBox1.TabIndex = 0;
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(284, 264);
             this.Controls.Add(this.textBox1);
             this.Cursor = System.Windows.Forms.Cursors.No;
             this.Name = "Form1";
             this.Text = "Form1";
             this.Activated += new System.EventHandler(this.Form1_Activate);
             this.Resize += new System.EventHandler(this.Form1_Resize);
             this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged);
             this.ResumeLayout(false);
             this.PerformLayout();
          }
          protected override void Dispose(bool disposing)
          {
             if (disposing && (components != null))
                components.Dispose();
             base.Dispose(disposing);
          }
          private System.ComponentModel.IContainer components = null;
    
          private System.Windows.Forms.TextBox textBox1 = null;
       }
    }
    

    Unless this is a particularly locked-down computer like a kiosk, users will probably hate it. Think about if they are needing to alt+tab to a different app to copy something to the clipboard to fill out your form… Just be careful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone give me some example code that creates a surface with a transparent
Can someone give me a code sample of 2-opt algorithm for traveling salesman problem.
Can someone give me an sample code for changing volume through a slider? I
Can someone give me a simple example involving threads in this manner, please. Problem
can someone give me a hint on how a histogram's pseudo code would look
Can someone find a strategy for this problem that DOESN'T INVOLVE CONVERTING TO BASE
Can someone give an example of a good time to actually use unsafe and
Can someone give some hints of how to delete the last n lines from
Can someone give me an example of how I would delete a row in
Can someone give me some info/background info on how I might go about writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.