Form border

myblueocean

Well-known member
Joined
Jun 29, 2007
Messages
161
Location
England
Programming Experience
Beginner
Can anyone give me a clue as to how to have a border around a certain piece of a form?

So nothing can pass from one part to another, like an invisible wall:D
 
All the containers (check the toolbar) can be useful in such a situation. Also check the anchor (property).

If you want more concrete help i guess you must be a bit more specific next time.

HTH :)
 
Right thank you for the reply.:cool: I'll get back if there are any problems.:rolleyes:
 
Well I have a 240x320 form, so how do I stop anything from going beyond that? Even by defining a resolution size you can still have things fly off it out of view?
 
Urm, anyone?

"Visual Basic (Declaration)
<LocalizableAttribute(True)> _
Public Overridable Property Anchor As AnchorStyles"

Thats in the help files, as well this.

"Visual Basic (Usage)
Dim instance As Control
Dim value As AnchorStyles

value = instance.Anchor

instance.Anchor = value"

Any hints, clues, small example?
 
Your question is like asking someone how to stop yourself walking outside your garden. If you don't want to walk beyond the boundaries of your property then just don't.

How can you possibly have something spontaneously fly off a form? If anything on a form moves then it must be your code doing it. If you don't want to move something beyond certain bounds then just don't. You can use the ClientSize property of a form or the Size property of a Panel or other container to determine where its boundaries are. Just don't move anything beyond those boundaries.
 
I would actually would like it that way, I don't want to press the key down and then it goes all the way to the bottom completely out of sight, because the form is set at a certain size.

As well, I'm going off topic here, but when i press key up or the left or right arrows they make my piece of graphics move?

I want the arrow up to make my piece of gfx go up too. I've given it a try, but it was a waste of time, can you give me a hint or something?
 
Like I said, if something is going off your form then it's your code doing it. If YOU don't want something to move above a Y value of zero then when the Y value is zero don't move it. So, you test the current Y value and you only reduce it if it's greater than zero. In the other direction the Y value will only be increased if the the Y value is less than the height of the form less the height of whatever it is you're moving. The very same principle applies to the X direction.
 
Alright I get back to you on the x, y problem.

Um, any ideas about how to get my piece of graphics going up?
 
Exactly what is a "piece of graphics"? Please explain what you want to happen and then we can explain how to achieve it. "how to get my piece of graphics going up" is a bit vague. Go up when? Initiated by the user or automatically? How was the "graphics" added to the form in the first place? Etc., etc. A clear explanation from you means a much better chance of a relevant response from us.
 
Last edited:
I thought at some point someone was going to point that out, I did try that but if I remember correctly it came up with it couldn't something identifer? or not declared.

I renamed them all rect2, and declared it at the top, ""Private rect2 As New Rectangle(0, 0, 25, 50)"

Or were you talking about "Dim oldRect As Rectangle2 = Me.rect2" ?

With, as "key up".
 
Follow these steps and you will see the principle in action. You should then be able to apply said principle to your own code.

1. Create a new WinForms project.
2. Add a Timer.
3. Add the following code:
VB.NET:
Private rect As New Rectangle(0, 0, 25, 25)
Private xIncrement As Integer = 1
Private yIncrement As Integer = 0

Private Sub Form1_Load(ByVal sender As Object, _
                       ByVal e As EventArgs) Handles MyBase.Load
    Me.Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, _
                        ByVal e As EventArgs) Handles Timer1.Tick
    'Mark the old rectangle to be erased.
    Me.Invalidate(Rectangle.Inflate(Me.rect, 1, 1))

    'Shift the rectangle.
    Me.rect.Offset(Me.xIncrement, Me.yIncrement)

    'Mark the new rectangle to be drawn.
    Me.Invalidate(Rectangle.Inflate(Me.rect, 1, 1))

    'Do the drawing.
    Me.Update()

    'Change the direction of movement if a boundary has been reached.
    If Me.xIncrement = 1 AndAlso Me.rect.Right >= Me.ClientSize.Width - 1 Then
        Me.xIncrement = 0
        Me.yIncrement = 1
    ElseIf Me.yIncrement = 1 AndAlso Me.rect.Bottom >= Me.ClientSize.Height - 1 Then
        Me.xIncrement = -1
        Me.yIncrement = 0
    ElseIf Me.xIncrement = -1 AndAlso Me.rect.Left <= 0 Then
        Me.xIncrement = 0
        Me.yIncrement = -1
    ElseIf Me.yIncrement = -1 AndAlso Me.rect.Top <= 0 Then
        Me.xIncrement = 1
        Me.yIncrement = 0
    End If
End Sub

Private Sub Form1_Paint(ByVal sender As Object, _
                        ByVal e As PaintEventArgs) Handles Me.Paint
    'Draw the rectangle.
    e.Graphics.DrawRectangle(Pens.Black, Me.rect)
End Sub
Now run the project and watch the square move around the edge of the form. You may want to make the form a bit smaller that the default size or it might take a while.
 
Thank you Jmcilhinney for answering that question.

"ClientSize.Width - 1 Then" is that the form?

How does it know the boundary?

"Private xincrement As Integer = 1
Private yincrement As Integer = 0"

What is increment?

x is horizonal, and y vertical. So ? you see my puzzled post.

And lastly, I didn't just copy all the code. The animation "If" parts I did. I was trying to take it in.

The If parts, are very new to me...so its kind of confusing at the moment.

I want to soon create a collision.:D
 
Back
Top