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
Related
i want to make title bar less resizable form.
so i add following code into form load event.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = String.Empty
Me.ControlBox = False
Me.FormBorderStyle = FormBorderStyle.SizableToolWindow
End Sub
this is vest way to make smoothly resizable form.
other every method which i use are not resize smoothly when try to resize from left or to side.
but there is a problem
upper margin of form is thick. how i solve this.
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
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)
I'm trying to make VB.net program and UI is like normal desktop. I have images in picture
boxes as desktop shortcuts. So user can drag them around in form. Btw im not skilled just started
to do things like this.
I can move picture boxes with mouse events its fine But pictures in pictureboxes are tearing when dragging. i have a big background picture, i guess this is the problem but i need help to get rid of tearing. heres code thx.
i also tryed as a panel and get same result.
Edit : pictures about dragging is png , 50x50 , transparent BG.
Public Class testform
Dim drag As Boolean
Dim mousex, mousey As Integer
Private Sub testform_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackgroundImage = My.Resources.worldmap 'approx 1.03mb Picture size
End Sub
Private Sub dragbox_MouseDown(sender As Object, e As MouseEventArgs) Handles dragbox.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
drag = True
mousex = Windows.Forms.Cursor.Position.X - dragbox.Left
mousey = Windows.Forms.Cursor.Position.Y - dragbox.Top
Else
End If
End Sub
Private Sub dragbox_MouseMove(sender As Object, e As MouseEventArgs) Handles dragbox.MouseMove
If drag = True Then
dragbox.Left = Windows.Forms.Cursor.Position.X - mousex
dragbox.Top = Windows.Forms.Cursor.Position.Y - mousey
End If
End Sub
Private Sub dragbox_MouseLeave(sender As Object, e As EventArgs) Handles dragbox.MouseLeave
drag = False
End Sub
End Class
You need to make the picture form double buffered. That means that it will no longer tear as it will first completely make sure it's all in place before showing the next frame.
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