Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# - Modifying Cursor.Clip function

Posted on 2007-04-10
3
Medium Priority
?
559 Views
Last Modified: 2008-01-09
Hi guys 'n gals,

I have a function which basically restricts Mouse movement to stay within a panel while dragging a PictureBox. I now want to modify this so that, rather than stopping the cursor from leaving the boundaries, I want to stop the PictureBox from leaving the boundaries (I don't want a PictureBox dropped half in and half out the panel).


Here is the existing code:

        void pb_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pbDown = true;
                if (selectedPB != null)
                {
                    selectedPB.BorderStyle = BorderStyle.None;
                    selectedPB.Location = new Point(selectedPB.Location.X + 1, selectedPB.Location.Y + 1);
                    selectedPB = null;
                }
                //selectedPB = (TransparentPictureBox)sender;
                selectedPB = (PictureBox)sender;
                selectedPB.BorderStyle = BorderStyle.FixedSingle;
                selectedPB.BringToFront();
                Rectangle pnl = this.RectangleToScreen(new Rectangle(panel1.Location, panel1.Size));
                pnl.X = pnl.X + 1;
                pnl.Width = pnl.Width - 2;
                pnl.Y = pnl.Y + 1;
                pnl.Height = pnl.Height - 2;
                Cursor.Clip = pnl;
                startX = e.X;
                startY = e.Y;
            }
        }




Cheers!!
0
Comment
Question by:Cyber-Drugs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 18886120
Hi Cyber-Drugs,

In the pb_MouseMove() event, calculate what the new position of the PB will be and adjust it if necessary...

Quick Rules:

    If pb.X < 0 Then pb.X = 0
    If pb.Y < 0 Then pb.Y = 0
    If pb.X + pb.Width > panel1.Width Then pb.X = panel1.Width - pb.Width
    If pb.Y + pb.Height > panel1.Height Then pb.Y = panel1.Height - pb.Height

Fleshed out:  (I think...my C# syntax sucks)

        void pb_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PictureBox localPB = (PictureBox)sender;
                if (localPB == selectedPB)
                {
                    Point pt = new Point(localPB.Location.X + (e.X - startX), localPB.Location.Y + (e.Y - startY));
                    if (pt.X < 0) { pt.X = 0; }
                    if (pt.Y < 0) { pt.Y = 0; }
                    if (pt.X + localPB.Width > panel1.Width) {
                        pt.X = panel1.Width - pb.Width;
                    }
                    if (pt.Y + localPB.Height > panel1.Height) {
                        pt.Y = panel1.Height - pb.Height;
                    }
                    localPB.Location = pt;
                }
            }
        }
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18886188
Hi Idle_Mind,

Thanks for the heads up on that, still getting to grips with understanding mouse restriction, the code you posted 50% works...

Top and Left work fine

Right and Bottom throw an error like this:

Object reference not set to an instance of an object.

Referencing these lines:

pt.X = panel1.Width - pb.Width;
and
pt.Y = panel1.Height - pb.Height;


any thoughts?
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18886237
changed "pb" for "localPB" and works fine, cheers!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question