how to move a picture box diagonally on button click vb 2010 - vb.net

I have a project that I am working on where I would like a picturebox to move diagonally on a button click. I've tried using PictureBox1.Left = 10 and PictureBox1.Top = 10 but that makes it so it goes left once and top for the rest of the button clicks. I would like it so when i click the button, the picturebox moves diagonally once and not multiple times separately. Is there any way where this is possible? This is the code I have so far
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
PictureBox1.Left -= 10
PictureBox1.Top = 10
End Sub
Added from comments
I've tried the timer but it made the same mistake as the button. This is the code for the timer.
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
PictureBox1.Left -= 10
PictureBox1.Top = 10
End Sub

The main problem in your code is you are moving the PictureBox to a fixed Y position = 10 and only changing it's x position by -10 each time.
So it doesn't move diagonally and it jumps to Y=10 and moves to left each time you click on button.
You need to set:
PictureBox1.Left -= 10
PictureBox1.Top -= 10 'Here you used PictureBox1.Top = 10
To show it as animation, you can use a timer with interval = 100 and change left and top -=1.
You can increase the speed by decreasing the interval and increasing the movement.

You can try setting the Location of your picturebox, instead of each axis individually. Something like this.
PictureBox1.Location = New Point(PictureBox1.Left - 10, PictureBox1.Top + 10)

Related

PictureBox scrolling past edges of form erases contents

I am trying to create a simple winform application in VB. I have a form, a picturebox with a background image and a button. When I press the button, I want the picturebox to move left across the form until it disappears off the left side. Then I want it to reappear on the right side and move to the left again.
Initial condition:
The problem seems to be that the areas of the picturebox that exceed the boundaries of the form get erased. So the first time around the images slides nicely off the left side of the form and never comes back to the right side. Or rather, it does come back, I just can see it.
Second scroll around, parts of image in picturebox that exceeded the boundaries of the form have been erased (left) or distorted (right):
In the code below I have deliberately changed the boundaries for the picturebox so that I can see that it is actually looping back around. But as you can see in this image, the parts of the picture box that exceeded the boundaries of the form are either erased or distorted.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Do
PictureBox1.Left -= 1
Threading.Thread.Sleep(2)
If PictureBox1.Left <= -20 Then 'If PictureBox1.Left <= PictureBox1.Width Then
PictureBox1.Left = Me.Width - 40 'PictureBox1.Left = Me.Width
End If
Loop
End Sub
End Class
I have tried this with both VS2019 and VS2017 on two different computers and the same thing happens. I have also tried replacing the picturebox with a textbox and much the same thing happens.
I changed my code to this, and now it works perfectly.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 25
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Left -= 1
If PictureBox1.Left <= -PictureBox1.Width Then
PictureBox1.Left = Me.Width
End If
End Sub
End Class

Keep Mouse Inside a WinForm Bounds

I have a Mouse Move method that moves a Button up and down along the Y axis on a WinForms project
When the Cursor is outside the bounds of the form the Button will not move
I would like to limit the Cursor from exiting or losing focus of the form
I would also like to set a limit on the top and bottom Y positions of the Button btnPad
Other Controls on the form are three buttons that form a TOP, BOTTOM and RIGHT WALL
The Y INSIDE dimensions of the TOP and BOTTOM walls are 50 and 690
FWIW the Form dimensions are Width 1000 and Height 890
I have some code that prevents the cursor from entering the form called MoveCursor
I will post some code I have tried like frmStart MouseLeave and MouseEnter as well as the actual MouseMove code
The Question is how to prevent the Cursor from exiting the Form?
Private Sub MoveCursor()
' Set the Current cursor, move the cursor's Position,
' and set its clipping rectangle to the form.
Me.Cursor = New Cursor(Cursor.Current.Handle)
Cursor.Position = New Point((Cursor.Position.X + 1000), (Cursor.Position.Y + 890))
Cursor.Clip = Me.Bounds
Cursor.Clip = New Rectangle(Me.Location, Me.Size)
End Sub
Private Sub frmStart_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)
Cursor.Position = New Point(X, Y)
Cursor.Clip = Me.Bounds
End Sub
Private Sub frmStart_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
MoveCursor()
btnPad.Top = e.Y
End Sub
The code below was as close as I could come to keeping the Cursor inside the play area which is the form the mouse still wanders out on the right side
Private Sub frmStart_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Cursor.Clip = New Rectangle(Me.Location, Me.Size)
btnPad.Top = e.Y
End Sub

Control flickers while its location is continuously changed

I have a button inside a form. The button moves horizontally from one point to another by continuously varying the Button.Location.X from 0 to 1000 using a timer. See code below.
Timer1.Interval = 100
Dim i as Integer = -1
Private Sub Timer1_tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Button1.Location = New Point(i, 50)
If i = 1000
i = 0
End If
i = i + 1
End Sub
Now the problem is while the button is moving it flickers. How do I stop the flickering?
Tried using Hans Passant's answer to this question but it's not working.

How to Zoom in and Zoom out in a Panel?

I have a Panel which have
Five labels in it
Two lines one is Vertical and other is Horizontal drawn at run time
An Ellipse drawn at run time
And Some Strings which are also drawn at run time
Now I want to zoom this Panel and every thing in it
How can I solve this problem?
First you have to set the Anchor to all 4 sides for all controls in the panel, resizing the panel will resize the controls also.
Here is the Code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Panel1.Width = Panel1.Width + 10
Panel1.Height = Panel1.Height + 10
End Sub

How to make form snap to certain heights when the form is resized vertically?

I have 3 PictureBoxes on a form that are tiled on top of each other. the form has a minimum vaule of (502, 416) and a maximum of (502, 1080).
because the user can select from a MenuStrip to display '1', '2' 0r '3' PictureBoxes at once '3' being the bottom and '1' being the top PictureBox. What i need is when the user drags down the form it snaps to the next PictureBoxes position, so it goes down in blocks this is what i have so far which is pretty far from working.
If Me.Height <= (1079) Then
Me.Height = (732)
ElseIf Me.Height <= (732) Then
Me.Height = (424)
ElseIf
...
End If
I also thought i might be able to figure it out if i new how to create a variable like this
If Me.Height <= (1079 to 733) Then
Me.Height = (732)
I know that it isn't the correct syntax but it's kind of the idea
If you can make sense from my not so good description and point me in the right direction/code example i will be most grateful :)
Thank you for your help
You could try using a Select Statement:
Select Case Me.Height
Case 425 To 732
Me.Height = 424
Case 733 To 1079
Me.Height = 732
End Select
To answer your further question. If you are going to use the Form Resize event any animation you have is going to get interesting because as you change the Form Height it will retrigger the Event. Personally if I were you I would stay with your initial idea of snapping to the next height and if you are wanting to animate the Form Height I would seriously look into WPF. But here is the SubRoutine that I said I would show you, I have used three buttons to intiatiate the resizing. Be carefull if you put this in your Form Resize Event if you don't block the event from rerunning the Subroutine it will freeze your computer.
Public Class Form1
Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
Me.Text = Me.Height
End Sub
Public Sub ChangeFormHeight(fromHeight As Integer, toHeight As Integer)
If fromHeight > toHeight Then
For newHeight As Integer = fromHeight To toHeight Step -1
Me.Height = newHeight
Next
Else
For newHeight As Integer = fromHeight To toHeight
Me.Height = newHeight
Next
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ChangeFormHeight(Me.Height, 424)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
ChangeFormHeight(Me.Height, 733)
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
ChangeFormHeight(Me.Height, 1080)
End Sub
End Class