Move a Picturebox within a Panel without it leaving bounds of Panel

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 21138 Views - Last Post: 18 May 2012 - 11:28 AM Rate Topic: -----

#1 aquahoya   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 15 May 2012 - 02:07 PM

Hello again DIC, :bigsmile:

I am looking for a way to prevent the pictureBox from leaving the bounds of a Panel after I click and drag.

I have so far coded the mouse_click and move of the picturebox, but when I move the pictureBox outside of the panel it leaves the panel; and that is what I am trying to avoid.

Is there any way to program this?
My code:
public partial class Form1 : Form
    {
        int x;
        int y;

        public Form1()
        {
            InitializeComponent();
        }


 private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = e.X;
                y = e.Y;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                pictureBox1.Left += (e.X - x);
                pictureBox1.Top += (e.Y - y);
            }

        }



Is This A Good Question/Topic? 0
  • +

Replies To: Move a Picturebox within a Panel without it leaving bounds of Panel

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,571
  • Joined: 12-June 08

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 15 May 2012 - 02:11 PM

Do a precheck - if the edge of the picture box less than or equal to the left of the panel then cancel the event.. top of the picture less than or equal to the top of the panel.. cancel.. etc.. etc..
Was This Post Helpful? 1
  • +
  • -

#3 aquahoya   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 15 May 2012 - 02:26 PM

Thanks for the reply modi123, but how do I code "if the edge of the picture box less than or equal to the left of the panel".

I figured that I should do something like that, but the coding behind it seems confusing to me. Do I use the panel's size? :helpsmilie:

I tried something like this:
if(pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds))
            {
Close();
            }
With Close(); to check if it actually did anything, and it sadly didn't do a single thing...

This post has been edited by aquahoya: 15 May 2012 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,571
  • Joined: 12-June 08

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 15 May 2012 - 02:47 PM

Are you reading up on what those methods do? Just bing "msdn <method name>" and the first hit or so is the .NET documentation.

Example:
http://msdn.microsof...rsectswith.aspx

Quote

Thanks for the reply modi123, but how do I code "if the edge of the picture box less than or equal to the left of the panel".

First let's think about it. First we know the coordinate system is 0,0 starting in the top left... panels are a form of container and we need to track their left edge... It would be easy to think "hey there's this "left" property on both picture box AND panel - let's use those!"... go a head.. try and if statement .

   Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

        If PictureBox1.Left > panel.left Then
            PictureBox1.Location = New Point(PictureBox1.Location.X - 5, PictureBox1.Location.Y)
        End If

    End Sub



Even better - put a break point there and see what happens! You know - basic Debugging
skills that you need to start developing or you'll never be able to track a bug down?

Say you plopped the panel down in the center of the screen.. about 150,150... that means relative to the form container the panel's left edge is 150.. (the form being 0 on the left hand side)... the picture box is inside the panel... but wait.. those gui controls go relative to the container they are in! So while it might be only hundred pixels to the right of the left edge of the panel (making it 250 relative to the form) the 'left' value for the picture box is just 100! Aaaaaaaaawww snap. The things we learn when we try out bits of our code!

In the example of above that means the left value of the pciture box is relative to the panel (not the form) and it thinks it is N pixels from zero.. zero being the left edge of that panel. Now in the panel's world it's left edge is relative to the form.. the form's edge being 0... So it comes down to scope....

With a little reworking, and understanding who is relative to what, we get this:

   Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

        If PictureBox1.Left > 10 Then  '-- I should use zero but I like to have a bit of an edge at first to make sure it stops.
            PictureBox1.Location = New Point(PictureBox1.Location.X - 5, PictureBox1.Location.Y)
        End If

    End Sub



With a little information on how to read up on the documentation and bravely trying out code (as you use the debugger) you can deduce that - when in a panel the picture box's left is relative to zero... (the left inside edge of the panel.. while the panel's left edge is relative to zero.. (that being the left inside edge of the form)! It's cool to test things out and read up on them!
Was This Post Helpful? 2
  • +
  • -

#5 aquahoya   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 15 May 2012 - 03:20 PM

Thanks for the in depth explanation modi. So intersection was not appropriate for this so I used what you suggested:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (pictureBox1.Left < panel1.Left)
                {
                    pictureBox1.Left += (e.X - xx);
                    pictureBox1.Top += (e.Y - yy);
                }
                
            }
            
        }



This somewhat works,(.Left refers to the right side of the control's for some reason tho).
When I drag the pictureBox to the right side of the panel, the pictureBox stops dragging the picturebox(great) but not immediately(not great)...

Another problem which is created by this is, I'm no longer able to to drag the picturebox at all now.

I'm still trying to figure out the code for this, I'm sure I'm close.
Was This Post Helpful? 0
  • +
  • -

#6 aquahoya   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 15 May 2012 - 04:03 PM

I'm still facing trouble :dontgetit: dang..
Was This Post Helpful? 0
  • +
  • -

#7 aquahoya   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 18 May 2012 - 07:25 AM

I am looking to create a collision method for when pictureBox1 collides with the Bottom & Right of panel1 on MouseMove.
I have created the collision for the Top & Left with help from a friend:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = e.X;
                y = e.Y;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (pictureBox1.Left + (e.X - x) > 0)
                        pictureBox1.Left += (e.X - x);

                if (pictureBox1.Top + (e.Y - y) > 0)
                        pictureBox1.Top += (e.Y - y);
                
            }

        }


But the collision with the .Bottom or .Right of a panel doesn't work the same way.
I heard the bottom of a control is calculated using .Top + .Height but I can't seem to make any sense of using that in my code :online2long:

Please help :flowers:
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,571
  • Joined: 12-June 08

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 18 May 2012 - 07:31 AM

.. well - where's your code you attempted to deal with the bottom and right? In what way was it not working?
Was This Post Helpful? 0
  • +
  • -

#9 aquahoya   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 18 May 2012 - 07:49 AM

View Postmodi123_1, on 18 May 2012 - 07:31 AM, said:

.. well - where's your code you attempted to deal with the bottom and right? In what way was it not working?

I first tried this
if (e.Button == MouseButtons.Left)
            {
                if (pictureBox1.Left + (e.X - x) > 0)
                        pictureBox1.Left += (e.X - x);

                if (pictureBox1.Top + (e.Y - y) > 0)
                        pictureBox1.Top += (e.Y - y);

                if (pictureBox1.Bottom + (e.Y - y) > 0)
                        pictureBox1.Bottom += (e.Y - y);

                if (pictureBox1.Right + (e.Y - y) > 0)
                        pictureBox1.Right += (e.Y - y);
                
            }


but that returned an error, then I tried
        int x;
        int y;
        int bottom;
        int right;

        public Form1()
        {
            InitializeComponent();
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                panel1.Left += (e.X - x);
                panel1.Top += (e.Y - y);
            }
        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Cursor = System.Windows.Forms.Cursors.SizeAll;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = e.X;
                y = e.Y;
                bottom = pictureBox1.Top + pictureBox1.Height;
                right = pictureBox1.Left + pictureBox1.Width;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (pictureBox1.Left + (e.X - x) > 0)
                        pictureBox1.Left += (e.X - x);

                if (pictureBox1.Top + (e.Y - y) > 0)
                        pictureBox1.Top += (e.Y - y);
                if (bottom > 0)
                    pictureBox1.Left += (e.X - x);
                if (right > 0)
                    pictureBox1.Top += (e.Y - y);
            }

        }


These are horribly incorrect solutions I've found out, and I still don't have the slightest clue as the answer to this. :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,571
  • Joined: 12-June 08

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 18 May 2012 - 07:52 AM

 if (pictureBox1.Bottom + (e.Y - y) > 0)
 pictureBox1.Bottom += (e.Y - y);


Explain to me what the logic of those lines are?

You area aware the top left corner is 0,0 - right?
Was This Post Helpful? 0
  • +
  • -

#11 aquahoya   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 18 May 2012 - 07:58 AM

View Postmodi123_1, on 18 May 2012 - 07:52 AM, said:

 if (pictureBox1.Bottom + (e.Y - y) > 0)
 pictureBox1.Bottom += (e.Y - y);


Explain to me what the logic of those lines are?

You area aware the top left corner is 0,0 - right?

Yes, but that was not the error that popped up. The error was that .Bottom was ReadOnly.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,571
  • Joined: 12-June 08

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 18 May 2012 - 07:59 AM

pictureBox1.Bottom += (e.Y - y);

Then why are you trying to increment the picture bottom? You should be incrementing the picture's x/y location ponit.
Was This Post Helpful? 0
  • +
  • -

#13 aquahoya   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 18 May 2012 - 08:08 AM

View Postmodi123_1, on 18 May 2012 - 07:59 AM, said:

pictureBox1.Bottom += (e.Y - y);

Then why are you trying to increment the picture bottom? You should be incrementing the picture's x/y location ponit.

I tried incrementing the x,y location point using:
if (pictureBox1.Right + (e.Y - y) > 0)
	                    (e.Y += y);


but it returned many errors...
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,571
  • Joined: 12-June 08

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 18 May 2012 - 08:11 AM

Do the properties 'bottom' or 'right' indicate they are anything more than read only? Nope.

http://msdn.microsof...picturebox.aspx

As I have said - use the 'location' property. create a new point object with the x, y you want, and set the location property to that point object.
http://msdn.microsof...l.location.aspx
Was This Post Helpful? 0
  • +
  • -

#15 aquahoya   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Move a Picturebox within a Panel without it leaving bounds of Panel

Posted 18 May 2012 - 08:20 AM

Yes you gave me this VB code to try, I'm not familiar with VB but I believe it would translate to this C# snippet:
if (pictureBox1.Left > panel1.left) 
{
pictureBox1.Location = new Point((pictureBox1.Location.X - 5), pictureBox1.Location.Y);
}



This caused the pictureBox1 to dissapear on mousemove.
Why can I not use the previous code I posted above?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3