Keep Picture box from leaving the form
Keep Picture box from leaving the form
Keep Picture box from leaving the form
Keep Picture box from leaving the form
Keep Picture box from leaving the form
Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form
Keep Picture box from leaving the form Keep Picture box from leaving the form
Keep Picture box from leaving the form
Go Back  Xtreme Visual Basic Talk > > > Keep Picture box from leaving the form


Reply
 
Thread Tools Display Modes
  #1  
Old 02-09-2011, 03:26 PM
VickiT VickiT is offline
Freshman
 
Join Date: Apr 2010
Posts: 27
Default Keep Picture box from leaving the form


Hey Everyone!

How do I keep my picture box from leaving the form screen and how do I make it 'bounce off the walls" when it hits one edge of the form?

Thanks!
Reply With Quote
  #2  
Old 02-10-2011, 02:02 PM
passel's Avatar
passelKeep Picture box from leaving the form passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

"...one edge of the form"

Which one?

Since this is a basic question, I'll assume you want a simple solution.
You could use two variable to hold the velocity of the picturebox, one for X velocity (left, right) and one for Y velocity(up,down).
Add a timer control and pick a reasonable interval (generally anything greater then 16 will work consistently across most current hardware). For instance, a value of 20 (milliseconds) would move the picturebox 50 times per second.
In the timer, you would adjust the picturebox's location by adding the X and Y velocity values to its position. Based on earlier posts, I assume you already know how to move the picturebox.
If you're moving to the right (X velocity is positive), then check to see if the right edge of the picturebox is equal to or greater than the right side (or width) of the client area of the form. If it is, reverse the sign of the X velocity (make it negative, in this case), so the box will be moving to the left the next time the timer event fires.
If you're moving down, check the bottom of the picturebox against the bottom (height) of the client area of the form.
If you're moving left, check the left edge of the picturebox to see if it is equal to or less than 0.
If you're moving up, check the top edge of the picturebox to see if it is equal to or less than 0.
In all cases, if you hit or went beyond the edge, reverse the sign of the cooresponding velocity so you "bounce" off that edge.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 02-10-2011 at 02:26 PM.
Reply With Quote
  #3  
Old 02-11-2011, 09:36 AM
VickiT VickiT is offline
Freshman
 
Join Date: Apr 2010
Posts: 27
Default

Thank you that was really helpful! but im still a little unsure

here's the code i have so far to make it stop on all 4 boundaries of the form.

Code:
 
pntLoc = New Point(pbxEnemy.Location.X - intEnemySpeed, pbxEnemy.Location.Y + intEnemySpeed)
        For Each Enemy1 In frmGame.lstEnemys
            If (Enemy1.pbxEnemy.Left + 1 < 0) Then
                'picturebox is about to move across the left side boundery
                intEnemySpeed = (-1)
            ElseIf (Enemy1.pbxEnemy.Left + Enemy1.pbxEnemy.Width + 1 > frmGame.Width) Then
                'picturebox is about to move across right side boundery
                intEnemySpeed = 1
            ElseIf Enemy1.pbxEnemy.Location.Y = 0 Then
                intEnemySpeed = 1
                'picturebox is about to move out of the top boundary
            ElseIf Enemy1.pbxEnemy.Location.Y = 322 Then
                'picturebox is about to move out of the bottom boundary
                intEnemySpeed = (-1)
            End If
        Next
the bottom boundary code doesn't seem right but it seems to work (all i did was experiment with different numbers until i found one that seemed to correspond to the bottom of the form. and the right might not be correct either

So right now it just bounces back and forth ... how do i make it bounce in a random/different direction after hitting the wall?

Thank you

Last edited by VickiT; 02-11-2011 at 10:09 AM.
Reply With Quote
  #4  
Old 02-13-2011, 02:23 PM
passel's Avatar
passelKeep Picture box from leaving the form passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

You are only using one variable, intEnemySpeed, to control both X and Y velocity.
So, your code can only move in two directions, back and forth along a diagonal line, because when you change the X or Y direction, you're also change the opposite Y or X.

You need two variables, one for Speed in the X direction and one for Speed in the Y direction.
If you want a single variable for "Speed", then that value should just represent the magnitude of the motion. You would multiply that value by an X and Y value for direction.
That X and Y value can be determined by a single "compass" heading, i.e. such as taking the Sine and Cosine values of the heading to determine Y and X values representing the relative motion of each to move that direction.

But, lets keep it simple for now. Just move in four diagonal directions.
So change your code to have a speed for both X and Y.

You can check for staying in the bounds of the form by using the ClientSize, which is the internal dimensions of the form area where drawing can take place.
A simple example where a picturebox is kept within the bounds of the form.
A value is maintained for both X and Y velocity.
If the box is going to go out in the X direction (either to the left or the right), then the X velocity is reversed.
The same for the Y velocity, if it will be moving out the top or bottom.
The example just requires a picturebox and a timer. The picturebox should have the backcolor changed, and/or border defined so you can see it move.
Code:
Public Class Form1

  Dim vx As Single = 1.0
  Dim vy As Single = 1.0

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Interval = 20
    Timer1.Start()
  End Sub

  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim l As Integer = PictureBox1.Left + vx
    Dim t As Integer = PictureBox1.Top + vy

    If (l <= 0) Or (l + PictureBox1.Width >= Me.ClientSize.Width) Then   'If the picturebox is going out the left or right then
      vx = -vx  'reverse the X velocity                                  '  Reverse the direction for the next movement
    Else                                                                 'Else
      PictureBox1.Left = l                                               '  Continue movement in the current direction
    End If

    If (t <= 0) Or (t + PictureBox1.Height >= Me.ClientSize.Height) Then   'If the picturebox is going out the top or bottom then
      vy = -vy  'reverse the Y velocity                                    '  Reverse the direction for the next movement
    Else                                                                   'Else
      PictureBox1.Top = t                                                 '  Continue movement in the current direction
    End If

  End Sub
End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 04-27-2011, 10:44 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

A slightly more basic approach to your question... if you want to bounce it off the walls like the screen saver on your DVD etc, you need to tell it how to move.

For example if you have a rectangle where the top and bottom are longer than the sides, set your parameters so it knows where the bottom, top and 2 sides are. i.e. Left = 20, Top = 20, Right = 500, Bottom = 200.

Start with your image in the top left hand corner (Left = 20, Top = 20 or X=20, Y=20). Now you need to tell it to move diagonally down towards the bottom right corner...

But before we do we need to set it up so it knows when to stop and change direction. So we might write something like this... (I wouldn’t use this but it is just to simplify the example).

Do While Bottom <> 200
PictureBox.Location = New Point(PictureBox.Location.X + 1, PictureBox.Location.Y + 1)
Loop

In order for it to move diagonally you need to move it to the right and drop it down, it hits the bottom (Bottom = 200) and exits the Loop. Because the Top and bottom are longer than the sides it will hit the Bottom rather than hitting the Bottom Right corner.

Now we have hit the bottom we want to send it towards the right wall, so we go Right and Up.

Do While Right <> 500
PictureBox.Location = New Point(PictureBox.Location.X + 1, PictureBox.Location.Y - 1)
Loop

Hit the right wall and now we head for the top, so that means Left and Up

Do While Top <> 20
PictureBox.Location = New Point(PictureBox.Location.X - 1, PictureBox.Location.Y - 1)
Loop

Hits the top and now we want to go Left and Down

Do While Left <> 20
PictureBox.Location = New Point(PictureBox.Location.X - 1, PictureBox.Location.Y - 1)
Loop

There is a small trap here... in the case of the first loop where we sent it to the bottom and that is fine it hit at X = 200, Y = 200, but then we sent it up and right towards the right wall... But note that the right wall is more than double the height (200 vs 500).

So it will miss the right wall altogether and hit the top, but now we don’t want to send it back left and down or it will go back towards the same spot on the bottom.

So instead it needs to be sent down and right changing the direction. Hopefully this little example highlights so of the potential pitfalls and things to look out for when setting up the PictureBox movement.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Keep Picture box from leaving the form
Keep Picture box from leaving the form
Keep Picture box from leaving the form Keep Picture box from leaving the form
Keep Picture box from leaving the form
Keep Picture box from leaving the form
Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form Keep Picture box from leaving the form
Keep Picture box from leaving the form
Keep Picture box from leaving the form
 
Keep Picture box from leaving the form
Keep Picture box from leaving the form
 
-->