VB.NET Custom progress bar - vb.net

I am trying to create a custom progress bar. For testing I have used two panels and a picturebox to move using a timer from left to right indicating a progress bar. My question is: how do I program this is to act as a progressbar with data - for example; If I have a variable that has the value 100 and another variable with the value 10, how do I show the progress bar as 10% complete?
`
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Panel4.Width = Panel4.Width - 1
Panel4.Location = New Point((Panel4.Location.X + 1), Panel4.Location.Y)
PictureBox2.Location = New Point((PictureBox2.Location.X + 1), PictureBox2.Location.Y)
Panel1.Width = Panel1.Width + 1
If Panel4.Width = 0 Then
Timer1.Stop()
End If
Catch ex As Exception
End Try
End Sub
`

Related

Label animation left and right inside the panel

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

Text scrolling up, but after closing the form. The text wont back on the start. vb.net

I was able to scroll the text up, put a stop on it when it reached a certain location on Y.
However, when I was trying to load the credit form again. It will not be back on the start.
Here's the code I'm working on.
Public Class creditsform
Private Sub creditsform_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y = 304) ' put back the label where it is.
End Sub
Private Sub creditsform_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y - 5) ' scroll up the label up
If (Label1.Location.Y = -1506) Then ' closes the form if he reach the end of credit roll
Timer1.Enabled = False ' disable the timer
Me.Close() ' then close the form
End If
End Sub
End Class
EDIT: HERE IS THE FIRST LOAD AND THE SECOND LOAD OF THE FORM.
Also after the 2nd load, it doesn't execute the if else statement to close automatically the form.
1ST LOAD & 2ND LOAD
Here's where creditsform called.
Public Class EndGame
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim Red, Green, Blue As Integer
Dim RandomNumGenerator As New Random
Red = RandomNumGenerator.Next(0, 255)
Green = RandomNumGenerator.Next(0, 255)
Blue = RandomNumGenerator.Next(0, 255)
Me.Label4.ForeColor = Color.FromArgb(Red, Green, Blue)
Me.Label5.ForeColor = Color.FromArgb(Red, Green, Blue)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click '' this button closes the form and shows the creditsform
Me.Close()
creditsform.ShowDialog()
End Sub
End class
If I understood the problem correctly since it's not very clear what you are trying to do.
This is normal your using a class but not creating new instance of it so your variables don't reset. you should try this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click '' this button closes the form and shows the creditsform
Me.Close()
Dim MyCredits As New creditsform
MyCredits.ShowDialog()
End Sub

How to make a label blink

I have a Stopwatch in my form with Interval = 1000 displayed in the hh:mm:ss format.
When it reaches the 5th second it should start to blink the label background as green but so far I can only make the background color turn to green without any flash.
This is how I turn the background color to green:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
If Label1.Text = "00:00:05" Then
Label1.BackColor = Color.Green
End If
End Sub
How do I make the label blink?
You could use a simple Async method to do this.
The following code will give Label1 the effect of flashing. Since we have used While True this will continue indefinitely once you hit "00:00:05".
Private Async Sub Flash()
While True
Await Task.Delay(100)
Label1.Visible = Not Label1.Visible
End While
End Sub
You would call this inside your Timer1_Tick method:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
If Label1.Text = "00:00:05" Then
Label1.BackColor = Color.Green
Flash()
End If
End Sub
If you only want to flash a couple of times we can make a simple change to Flash():
Private Async Sub Flash()
For i = 0 To 10
Await Task.Delay(100)
Label1.Visible = Not Label1.Visible
Next
'set .Visible to True just to be sure
Label1.Visible = True
End Sub
By changing the number 10 to a number of your choice you can shorten or lengthen the time taken to flash. I have added in Label1.Visible = True after the For loop just to be sure that we see the Label once the flashing has finished.
You will have to import System.Threading.Tasks to make use of Task.Delay.
You need a label, two textboxes, and a button.
The screen allows you to 'set' a couple of colours - this could be taken further, by adding an Error colour, a Warning colour (where you haven't filled a field in...?) and more.
This colour selection would, in a real application, be done by an admin person, from a separate screen, and stored in the DB.
The timer frequency would also be set in the Admin screen/function.
This particular screen needs the textboxes to be double-clicked, and a colour selected for each one.
The back colour for each box changes. Then press the Start button.
If you press the Start button again, it toggles the timer (on/off)
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' not quite correct for what I want, but close...
' https://bytes.com/topic/visual-basic-net/answers/368433-blinking-text
Me.Label1.Text = "A blinking text box"
Me.Label1.BackColor = TextBox2.BackColor
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Me.Label1.BackColor = TextBox2.BackColor Then
Me.Label1.BackColor = TextBox1.BackColor
Else
Me.Label1.BackColor = TextBox2.BackColor
End If
End Sub
Private Sub TextBox1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDoubleClick
Dim dlg As New ColorDialog()
If dlg.ShowDialog() = DialogResult.OK Then
TextBox1.BackColor = dlg.Color
End If
End Sub
Private Sub TextBox2_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TextBox2.MouseDoubleClick
Dim dlg As New ColorDialog()
If dlg.ShowDialog() = DialogResult.OK Then
TextBox2.BackColor = dlg.Color
End If
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Timer1.Enabled = Not Timer1.Enabled
End Sub
End Class
Try to put something like this in Timer1_Tick event handler -
Label1.Visible = Not Label1.Visible
Set the timer to enabled and it will do the job.
If you specify the color when the Text is 00:00:05 then you should also specify what the Backcolor should be when the text is something else i.e 00:00:06
Try this and see if it works:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
If Label1.Text = "00:00:05" Then
Label1.BackColor = Color.Green
else
Label1.Backcolor = Color.Yellow '(Change color as needed)
End If
End Sub

How to fill a progress bar by clicking in VB.NET?

I'm making a game in VB.Net and I'm not familiar with the progress bar. I need something where the player need to press a button as fast as they can to fill up the progress bar and proceed to the next leve or if not fast enough then lose.I have no code for this because I don't know how to build up something like this. Any help would be grate.
Thanks
Say you have a button, Button1, and you have a progressbar, ProgressBar1.
You can add to the progressbar's value everytime you click on Button1 by using the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ProgressBar1.Value + 1 < ProgressBar1.Maximum Then
ProgressBar1.Value += 1
End If
End Sub
Now, notice the condition I wrapped the increment code with. This will make sure that the user does not surpass the maximum value allowed in the progressbar1.
Edit:
As for the rest of your program, you will need to use a timer in order to track the time.
for the proceed button, you will need to use the visible property which exists on buttons in order to hide a button until some condition is met.
Re-Edit:
Public Class Form1
Private intCurrentTime As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ProgressBar1.Value + 1 < ProgressBar1.Maximum Then
ProgressBar1.Value += 1
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If intCurrentTime = 10 Then
If ProgressBar1.Value = ProgressBar1.Maximum Then
'SHOW "Proceed" BUTTON
Else
MsgBox("You have failed!")
End If
intCurrentTime = 0
Else
intCurrentTime += 1
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
End Class

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