Make a Auto slide show - vb.net

I am trying to create slide show I loaded the picture in picture box 2 through form load and in the timer options left him enabled here is my code and for some reason it is not changing pictures I also tried to do it with out loading a picture through form load same thing not changing just blank
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim max As Integer = 10000
Dim rnd As New Random
Dim rand As Integer = rnd.Next(10, max + 1)
Dim i As Integer = 1
Dim number(max - 1) As Integer
For i = 0 To max - 1
If number(i) = rand Then
rand = rnd.Next(1, max + 1)
i = -1
ElseIf number(i) = 0 Then
number(i) = rand
rand = rnd.Next(1, max + 1)
If i = max - 1 Then
Exit For
End If
i = -1
End If
Next
Timer1.Interval = number(i)
i += 1
ChangeImage()
End Sub
Private Sub ChangeImage()
Static Dim iImage1 As Integer
Select Case iImage1
Case 0
PictureBox2.Image = My.Resources.Image2
iImage1 += 1
Case 1
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
iImage1 += 1
Case 2
PictureBox2.Image = My.Resources.Image4
iImage1 += 1
Case 3
PictureBox2.Image = My.Resources.Image5
iImage1 += 1
Case 4
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
iImage1 += 1
End Select

I want just to change a picture every 10 seconds in picture box2
Make sure the Interval property of your Timer is set to 10,000.
Then use code like this:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Static rnd As New Random
Dim i As Integer = rnd.Next(5) ' 0 to 4 inclusive
Select Case i
Case 0
PictureBox2.Image = My.Resources.Image2
Case 1
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
Case 2
PictureBox2.Image = My.Resources.Image4
Case 3
PictureBox2.Image = My.Resources.Image5
Case 4
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
End Select
End Sub
But I suspect there is more to this story. What were you trying to do with the Interval?

It's because you dont pass the parameter.
In your sub ChangeImage, you set a new variable.
Change like this :
ChangeImage(i)
End Sub
Private Sub ChangeImage(iImage1)
Select Case iImage1

Related

Restarting image loop

So I'm doing a project for school and I have an image loop for my program with the code as follows
Dim Images(10) As Bitmap
Dim Pos As Integer = 0
Private Sub MainMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Inserting images from resources
Images(0) = GloriousGetaways.My.Resources.MM10
Images(1) = GloriousGetaways.My.Resources.MM1
Images(2) = GloriousGetaways.My.Resources.MM2
Images(3) = GloriousGetaways.My.Resources.MM3
Images(4) = GloriousGetaways.My.Resources.MM4
Images(5) = GloriousGetaways.My.Resources.MM5
Images(6) = GloriousGetaways.My.Resources.MM6
Images(7) = GloriousGetaways.My.Resources.MM7
Images(8) = GloriousGetaways.My.Resources.MM8
Images(9) = GloriousGetaways.My.Resources.MM9
'Puts the images into order
PictureBox1.Image = Images(Pos)
End Sub
Private Sub MainmenuSlideshowTimer_Tick(sender As Object, e As EventArgs) Handles MainmenuSlideshowTimer.Tick
'Starting the timer for the slideshow on main menu
MainmenuSlideshowTimer.Start()
'Setting the time between slides to 5 seconds
MainmenuSlideshowTimer.Interval = 5000
Pos = Pos + 1
If Pos < Images.Length - 1 Then
PictureBox1.Image = Images(Pos)
Else
Pos = Images.Length - 2
End If
End Sub
It works perfectly but the slideshow stops once it gets to the last image on the form. How would I make it so it continuously loops and restarts the slideshow from the first image once it gets to the last image. I'm not very advanced so I'm unsure.
Have a good day
Arrays in vb.net Are declared Images(UpperBound) As Type. So, Images(9).
GloriousGetaways.My.Resources.MM10 looks a bit strange. Are you sure you are getting the images you expect?
You want to check if Pos is greater than Images.Lenght -1 and reset to zero. Increment the Pos as the last line of the Tick event.
Dim Images(9) As Bitmap
Dim Pos As Integer
Private Sub MainMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Inserting images from resources
Images(0) = GloriousGetaways.My.Resources.MM10
Images(1) = GloriousGetaways.My.Resources.MM1
Images(2) = GloriousGetaways.My.Resources.MM2
Images(3) = GloriousGetaways.My.Resources.MM3
Images(4) = GloriousGetaways.My.Resources.MM4
Images(5) = GloriousGetaways.My.Resources.MM5
Images(6) = GloriousGetaways.My.Resources.MM6
Images(7) = GloriousGetaways.My.Resources.MM7
Images(8) = GloriousGetaways.My.Resources.MM8
Images(9) = GloriousGetaways.My.Resources.MM9
MainmenuSlideshowTimer.Interval = 5000
MainmenuSlideshowTimer.Start()
PictureBox1.Image = Images(Pos)
End Sub
Private Sub MainmenuSlideshowTimer_Tick(sender As Object, e As EventArgs) Handles MainmenuSlideshowTimer.Tick
If Pos > Images.Length - 1 Then
Pos = 0
End If
PictureBox1.Image = Images(Pos)
Pos += 1
End Sub

How can I make this label to just show different string values at each case

Take a look at the simple code below. It is suppose to show string values at different case but the output is only the first one: "what is your email". Please, I need explanation.
The objective is to change the labll text upon click event on next button on the form.
Public Class Form4PassworRecovery
Dim counter As Integer = 0
Private Sub Button1Next_Click(sender As Object, e As EventArgs) Handles Button1Next.Click
Label1Intro.Hide()
Select Case counter
Case 0
Question("What is your Email?")
Case 1
Question("What is your favorite Hobby")
Case 2
Question("What is your minor")
End Select
counter += 1
Answer()
End Sub
Sub Answer()
Dim A As New TextBox
A.Location = New Point(66.5, 120)
A.ForeColor = Color.White
A.BackColor = Color.FromArgb(153, 217, 255)
A.Size = New Point(400, 29)
GroupBox1.Controls.Add(A)
A.Show()
End Sub
Sub Question(ByVal Question As String)
Dim Q As New Label
Q.Text = Question
Q.Location = New Point(66.5, 90)
Q.Size = New Point(400, 29)
Q.ForeColor = Color.White
Q.BackColor = Color.FromArgb(153, 217, 255)
GroupBox1.Controls.Add(Q)
Q.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "Cancel" Then
Me.Hide()
Me.Dispose()
End If
End Sub
End Class
Am expecting the output of the form to be different string value at each case but I kept receiving only the first string value.
You can store the locations on your form and then increment them as you place each Label and TextBox pairing:
Dim counter As Integer = 0
Dim labelLocation As Point = New Point(5, 5)
Dim textBoxLocation As Point = New Point(5, 30)
Private Sub Button1Next_Click(sender As Object, e As EventArgs) Handles Button1Next.Click
Label1Intro.Hide()
Select Case counter
Case 0
Question("What is your Email?")
Case 1
Question("What is your favorite Hobby")
Case 2
Question("What is your minor")
End Select
counter += 1
Answer()
End Sub
Sub Question(ByVal Question As String)
Dim Q As New Label
Q.Text = Question
Q.Location = labelLocation
Q.Size = New Size(400, 29)
Q.ForeColor = Color.White
Q.BackColor = Color.FromArgb(153, 217, 255)
GroupBox1.Controls.Add(Q)
Q.Show()
textBoxLocation = New Point(labelLocation.X, labelLocation.Y + Q.Height + 7)
labelLocation = New Point(labelLocation.X, textBoxLocation.Y + 7 + Q.Height)
End Sub
Sub Answer()
Dim A As New TextBox
A.Location = textBoxLocation
A.ForeColor = Color.White
A.BackColor = Color.FromArgb(153, 217, 255)
A.Size = New Size(400, 29)
GroupBox1.Controls.Add(A)
A.Show()
End Sub

Automatically increment selected row through DataGridView

I am trying to update a selected row to a number and increment every other row after that by 1 from the newly updated value, leaving all previous rows unaffected
I have the selected row to update figured out:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim i = DataGridView1.CurrentRow.Index
With DataGridView1
.Rows(i).Cells("Value").Value = NumTextbox.Text
End With
I tried adding:
.Rows(i).Cells("Value").Value = i + 1
But only labels the value to the number of the row, can anyone point me in the right direction?
Putting it all together...
Private Sub IncrementRows()
Dim i = DataGridView1.CurrentRow.Index
Dim num As Integer
If Integer.TryParse(NumTextbox.Text, num) Then
With DataGridView1
.Rows(i).Cells("Value").Value = num
End With
For j As Integer = i + 1 To DataGridView1.Rows.Count - 1
num += 1
DataGridView1.Rows(j).Cells("Value").Value = num
Next
Else
MessageBox.Show("Please enter a valid number.")
NumTextbox.Focus
End If
End Sub
For j As Integer = i + 1 to DataGridView1.Rows.Count - 1
DataGridView1.Rows(j).Cells("Value").Value = i + 1
next

Fading the color of a label from 1 to the other

I have a very simple countdown timer that I made. I would like the color of the label (numbers) to change as the timer ticks. I want to start with Green and then fade(transition) to Red when the timer ticks 00:00.
The timer is working great, I can also get the label to change to red once the timer hits 00:00. I would like it to fade though. Here's is part of the code.
'handles the label ticking down'
Private Sub tmrCountdown_Tick(sender As Object, e As EventArgs) Handles tmrCountdown.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.Milliseconds > 0 Then
lblTime.Text = ts.ToString("mm\:ss")
lblTime.ForeColor = Color.FromArgb(0, 255, 0)
Else
lblTime.ForeColor = Color.FromArgb(255, 0, 0) 'changes label color to red when it hits 00:00'
'stops the timer once the label reaches 00:00
lblTime.Text = "00:00"
'Plays sound when timer hits 00:00'
My.Computer.Audio.Play(My.Resources.alarm, AudioPlayMode.BackgroundLoop)
tmrCountdown.Stop()
End If
End Sub
I know this is an old thread, but I was looking for something like this with a label glowing red. To do this I created a timer and then set up some conditional counters to alter the RGB values of the label colour. The rate of change is set by the timer.interval value. Hope this helps someone.
Public Class AlertWindow
Dim Red As Integer = 0
Dim Green As Integer = 0
Dim Blue As Integer = 0
Dim CountUp As Boolean = True
Private Sub AlertWindow_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If CountUp = True Then
If Red < 253 Then
Red = Red + 1
Else
CountUp = False
End If
End If
If CountUp = False Then
If Red > 0 Then
Red = Red - 1
Else
CountUp = True
End If
End If
Label1.ForeColor = Color.FromArgb(Red, Blue, Green)
End Sub
End Class
For Fade in you can use Alpha parameter
for example:
Static Alpha As Integer
lblTime.BackColor = Color.FromArgb(Alpha , 255, 0, 0)
Alpha += 5 'amount of opacity change for each timer tick
If Alpha > 255 Then lblTime.Enabled = False 'finished fade-in
This works better when you can average the colors together.
You have to work out what value you want for the tickCount based on your timer interval:
Private tickCount As Integer = 100
Private tickValue As Integer = 0
Private Sub tmrCountdown_Tick(sender As Object, e As EventArgs) _
Handles tmrCountdown.Tick
If tickValue > tickCount Then
tmrCountdown.Stop()
tickValue = 0
Else
lblTime.ForeColor = AvgColor(Color.Green, Color.Red, tickValue, tickCount)
tickValue += 1
End If
End Sub
Private Function AvgColor(startColor As Color,
finalColor As Color,
colorValue As Integer,
colorCount As Integer) As Color
Dim r1 As Integer = startColor.R
Dim g1 As Integer = startColor.G
Dim b1 As Integer = startColor.B
Dim r2 As Integer = finalColor.R
Dim g2 As Integer = finalColor.G
Dim b2 As Integer = finalColor.B
Dim avgR As Integer = r1 + ((r2 - r1) * colorValue) / colorCount
Dim avgG As Integer = g1 + ((g2 - g1) * colorValue) / colorCount
Dim avgB As Integer = b1 + ((b2 - b1) * colorValue) / colorCount
Return Color.FromArgb(avgR, avgG, avgB)
End Function
'RGB_1 = Timer;
'RGB_2 = Timer;
'RGB_3 = Timer;
'Label1 = Label;
'RGB1 = Progressbar;
'RGB2 = Progressbar;
'RGB3 = Progressbar.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RGB1.Value = 255
RGB_1.Interval = 10
RGB_2.Interval = 10
RGB_3.Interval = 10
RGB_1.Start()
End Sub
Private Sub RGB_1_Tick(sender As Object, e As EventArgs) Handles RGB_1.Tick
If RGB2.Value = 255 Then
If RGB1.Value = 0 Then
If RGB3.Value = 255 Then
RGB_1.Stop()
RGB_2.Start()
Else
RGB3.Value += 1
End If
Else
RGB1.Value -= 1
End If
Else
RGB2.Value += 1
End If
SetColor()
End Sub
Private Sub RGB_2_Tick(sender As Object, e As EventArgs) Handles RGB_2.Tick
If RGB3.Value = 255 Then
If RGB2.Value = 0 Then
If RGB1.Value = 255 Then
RGB_2.Stop()
RGB_3.Start()
Else
RGB1.Value += 1
End If
Else
RGB2.Value -= 1
End If
Else
RGB3.Value += 1
End If
SetColor()
End Sub
Private Sub RGB_3_Tick(sender As Object, e As EventArgs) Handles RGB_3.Tick
If RGB3.Value = 0 Then
RGB_3.Stop()
RGB_1.Start()
Else
RGB3.Value -= 1
End If
SetColor()
End Sub
Sub SetColor()
Label1.ForeColor = Color.FromArgb(RGB1.Value, RGB2.Value, RGB3.Value)
End Sub

VB.Net ComboBox.SelectedIndex always reverts to 1

I am writing a Rock-Paper-Scissors game and have to use a ComboBox to get the user's selection. The problem is that no matter what I select, the selected index always reverts to 1, thus the user always selects rock. How can I fix this?
Imports System.Random
Public Class Form1
Dim played As Integer = 0
Dim won As Integer = 0
Dim lost As Integer = 0
Dim draw As Integer = 0
Dim percent As Single = 0.0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As New Random()
Dim b As Integer = a.Next(1, 4)
Dim selection As Integer
ComboBox1.SelectedIndex = selection
If b = 1 Then
Label3.Text = "Rock"
Label3.Visible = True
Select Case selection
Case selection = 1
MsgBox("Draw.")
draw += 1
Case selection = 2
MsgBox("Paper Covers Rock. You win!")
won += 1
Case selection = 3
MsgBox("Rock Crushes Scissors. You lose.")
lost += 1
End Select
ElseIf b = 2 Then
Label3.Text = "Paper"
Label3.Visible = True
Select Case selection
Case selection = 1
MsgBox("Paper Covers Rock. You lose.")
lost += 1
Case selection = 2
MsgBox("Draw.")
draw += 1
Case selection = 3
MsgBox("Scissors Cuts Paper. You win!")
won += 1
End Select
ElseIf b = 3 Then
Label3.Text = "Scissors"
Label3.Visible = True
Select Case selection
Case selection = 1
MsgBox("Rock Crushes Scissors. You win!")
won += 1
Case selection = 2
MsgBox("Scissors Cuts Paper. You lose.")
lost += 1
Case selection = 3
MsgBox("Draw.")
draw += 1
End Select
End If
played += 1
percent = (won / played) * 100
PlayedText.Text = played.ToString
WonText.Text = won.ToString
LostText.Text = lost.ToString
DrawText.Text = draw.ToString
PercentText.Text = percent.ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
I think you need to change this line to
Dim selection As Integer
selection = ComboBox1.SelectedIndex
and the syntax for the SELECT....CASE is
Select Case selection
Case 1
MsgBox("Draw.")
draw += 1
Case 2
MsgBox("Paper Covers Rock. You win!")
won += 1
Case 3
MsgBox("Rock Crushes Scissors. You lose.")
lost += 1
End Select