Label animation left and right inside the panel - vb.net

I am doing a simple animation using a label panel and timer, how do I animate the label to left and right inside the panel when the form is open?
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Label2.Right < 0 Then
Label2.Left = Panel1.ClientSize.Width
Else
Label2.Left -= 10
End If
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label2.Parent = Panel1
Label2.Location = New Point(Panel1.ClientSize.Width,
Panel1.ClientSize.Height / 2 - (Label2.Height / 2))
Timer1.Start()
End Sub

Related

How to save location of movable button in visual basic winform?

I have a movable button. the new location is save in my setting once it is mouse up event. when I move the button, sometimes the button disappear, after I re run it, it completely disappear.
How to save the button location before I close the app?
Public Class Form1
Dim x, y As Integer
Dim newpoint As New Point
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.button1_x70123454440211bghff > 0 Or My.Settings.button1_y70123454440211bghff > 0 Then
Me.Button1.Location = New Point(My.Settings.button1_x70123454440211bghff, My.Settings.button1_y70123454440211bghff)
End If
End Sub
Private Sub Button1_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
newpoint = Control.MousePosition
newpoint = Control.MousePosition
newpoint.X -= x
newpoint.Y -= y
Button1.Location = newpoint
End If
End Sub
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
x = Control.MousePosition.X - Button1.Location.X
y = Control.MousePosition.Y - Button1.Location.Y
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
My.Settings.button1_x70123454440211bghff = x
My.Settings.button1_y70123454440211bghff = y
My.Settings.Save()
End Sub
Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
My.Settings.richtextbox1 = RichTextBox1.Text
End Sub
End Class
Follow this answer for button movement, and add some checks to keep the control within its parent container. For the setting, I like to keep things simple, and will take advantage of the ability of Visual Studio to store a struct in the settings, so instead I will use a System.Drawing.Point
Now you ask "How to save the button location before I close the app?" Well you can do it in Form_Close, or do it in the MouseUp event. Since you do it in the MouseUp, I will do the same. And I will load the last position in Form_Load
Public Class Form1
Private initialCursorPosition As System.Drawing.Point
Private intialButtonPosition As System.Drawing.Point
Private buttonIsMoving As Boolean = False
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
Dim b = DirectCast(sender, Button)
initialCursorPosition = System.Windows.Forms.Cursor.Position
intialButtonPosition = b.Location
buttonIsMoving = True
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
buttonIsMoving = False
My.Settings.Button1Location = DirectCast(sender, Button).Location
End Sub
Private Sub Button1_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove
If buttonIsMoving Then
Dim b = DirectCast(sender, Button)
Dim newX = intialButtonPosition.X - (initialCursorPosition.X - Cursor.Position.X)
Dim newY = intialButtonPosition.Y - (initialCursorPosition.Y - Cursor.Position.Y)
newX = Math.Max(Math.Min(newX, b.Parent.ClientSize.Width - b.Width), 0)
newY = Math.Max(Math.Min(newY, b.Parent.ClientSize.Height - b.Height), 0)
b.Location = New Point(newX, newY)
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button1.Location = My.Settings.Button1Location
End Sub
End Class

Moving PictureBox with Timer

I want to create a program wherein when I click a Button, the PictureBox that is of the same width and height as the form moves down but I want the Timer to stop right after the PictureBox leaves the frame/form. And when I click another Button, the PictureBox will move back up but it will stop when it's at the center of the form, basically at the same location it was before moving down. The form's size is 700, 1000 if that helps. This is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 9)
If (PictureBox1.Location = New Point(700, 1100)) Then
Timer1.Enabled = False
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer2.Enabled = True
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 9)
If (PictureBox1.Location = New Point((Me.Width / 700) - (PictureBox1.Width / 700), (Me.Height / 1000) - (PictureBox1.Height / 1000))) Then
Timer2.Enabled = False
End If
End Sub
Let's assume your PictureBox starts in the top, left corner of the containing control (i.e. the Form, or a Panel, or whatever). This is Point(0,0).
In this event handler...
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 9)
If (PictureBox1.Location = New Point(700, 1100)) Then
Timer1.Enabled = False
End If
End Sub
...you are checking if the top left corner of PictureBox1 is at position 700,1100 instead of checking if it is at 0,1100. Also, since you're adding + 9 each timer tick, it'll never be at a Y position of exactly 1100.
And then in this event...
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 9)
If (PictureBox1.Location = New Point((Me.Width / 700) - (PictureBox1.Width / 700), (Me.Height / 1000) - (PictureBox1.Height / 1000))) Then
Timer2.Enabled = False
End If
End Sub
You want to check if PictureBox1.Location is now 0,0 (the starting position) instead of all of that position math you are doing.
Here is a cleaned-up version of your code. Note that it first checks the position of the PictureBox and only moves it if necessary.
Private Const INCREMENT As Integer = 9
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If PictureBox1.Location.Y >= 1100 Then
Timer1.Enabled = False
Else
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + INCREMENT)
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer2.Enabled = True
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If PictureBox1.Location.Y <= 0 Then
Timer2.Enabled = False
Else
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - INCREMENT)
End If
End Sub

Change PictureBox Visibility After Certain Time VB.Net

I currently have it to where picturebox1 is visible on loadup and I would like to change it to where picturebox2 is visible and picturebox1 is not after 3 seconds. I have been unable to get this to visibly work. Any suggestions? I have looked around and saw the Picturebox.refresh & picturebox.update but have not been able to get these to work. I am open to suggestions on how to do this differently as well. Thanks for the help!
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.BackgroundImage = My.Resources.Resources._024689
PictureBox2.BackgroundImage = My.Resources.Resources._152522206296244269
PictureBox1.Visible = True
PictureBox2.Visible = False
InitializeComponent()
'starts timer
StartTimer.Interval = 1000
StartTimer.Start()
End Sub
Private Sub StartTimer_Tick(sender As Object, e As EventArgs) Handles StartTimer.Tick
time += 1
Debug.Print("Time = " & time)
If time = 3 Then
PictureBox2.Visible = True
PictureBox1.Visible = False
StartTimer.Stop()
End If
End Sub
For the record, this worked exactly as expected:
'Ensure that resources are loaded once only.
Private ReadOnly firstImage As Image = My.Resources.Capture__56x81_
Private ReadOnly secondImage As Image = My.Resources.Capture__70x264_
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.Image = firstImage
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
PictureBox1.Image = secondImage
End Sub
Note that the Interval of Timer1 was set to 3000 in the designer.

Panel Location Sliding Effect in VB.NET

what's wrong in my code? my syntax is right, the logic and concept is right.
but when i run the program and pressed the button(that make the panel slides UPWARD) the panel doesn't stop. even though I put a condition inside the timer.
here's the code;
Private Sub btn_existing_Click(sender As Object, e As EventArgs) Handles btn_existing.Click
timerPanelSlider3.Start()
pnl_info1.Enabled = False
pnl_options.Enabled = False
End Sub
Private Sub timerPanelSlider3_Tick(sender As Object, e As EventArgs) Handles timerPanelSlider3.Tick
yy -= 5
pnl_existing.Location = New Point(4, yy)
'pnl_existing.BringToFront()
If pnl_existing.Location.Y = 225 Then
timerPanelSlider3.Stop()
End If
End Sub
Your Location.Y is not hitting 225 you can check if it gets below 225. Or change it's starting position to 625.
Private Sub btn_existing_Click(sender As Object, e As EventArgs) Handles btn_existing.Click
timerPanelSlider3.Start()
pnl_info1.Enabled = False
pnl_options.Enabled = False
End Sub
Private Sub timerPanelSlider3_Tick(sender As Object, e As EventArgs) Handles timerPanelSlider3.Tick
yy -= 5
pnl_existing.Location = New Point(4, yy)
'pnl_existing.BringToFront()
If pnl_existing.Location.Y < 225 Then
timerPanelSlider3.Stop()
End If
End Sub

How to generate multiple picture boxes and control them using a timer?

My uni asked us to make a game using VB, and I really don't know much about the language.
I'm trying to make a game where balloons go up to them top of the screen and must be popped before getting there.
Public Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If PictureBox1.Top = 0 Then
PictureBox1.Visible = False
Timer1.Enabled = False
End If
PictureBox1.Top = PictureBox1.Top - 1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
PictureBox1.Visible = False
End Sub
End Class
This is my code so far, when I click the button, the balloon starts to go up, if I click the balloon, it disappears, it also disappears if it reaches the top and the timer stops.
How can I generate more balloons and control them using that timer?
Now all you have let to do is add the functionality of adding more PictureBoxes, maybe a second timer and when you create them use an Addhandler statement to point them the the pbs_Click event that I made and add them to the List I made as well.
Public Class Form1
Private PBs As New List(Of PictureBox)
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For Each pb As PictureBox In PBs
If pb.Top = 0 Then
pb.Visible = False
Timer1.Enabled = False
Else
pb.Top = pb.Top - 1
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1000'ms
Timer1.Enabled = True
End Sub
Private Sub pbs_Click(sender As Object, e As EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
PBs.Remove(pb)
End Sub
Private Sub makeNewPB()
Dim pb As New PictureBox
Addhandler pb.Click, AddressOf pbs_Click
'don't forget to make them the size you need
PBs.Add(pb)
End Sub
End Class