Loading and viewing images from imagelist - vb.net

As you see the code given below is not very useful. Is it possible to shorten the code. Mousewheel back and forward give the same result (next image). Keydown cannot be configured.
Private Sub Images_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
count += 1
If count + 1 > ImageList1.Images.Count Then
count = 0
End If
PictureBox1.Image = ImageList1.Images.Item(count)
End Sub
Private Sub Images_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel
count += 1
If count + 1 > ImageList1.Images.Count Then
count = 0
End If
PictureBox1.Image = ImageList1.Images.Item(count)
End Sub
Private Sub Images_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Down Then
count += 1
If count + 1 > ImageList1.Images.Count Then
count = 0
End If
PictureBox1.Image = ImageList1.Images.Item(count)
End If
End Sub

Here is a way to shorten this, just use function:
Private Sub Images_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
End Sub
Private Sub Images_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel
If e.Delta > 0 Then
PictureBox1.Image = ImageList1.Images.Item(decreaseCount(count))
ElseIf e.Delta < 0 Then
PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
End If
End Sub
Private Sub Images_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Down Then
PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
End If
End Sub
Private Function increaseCount(ByRef count as integer) As Integer
count += 1
If count + 1 > ImageList1.Images.Count Then
count = 0
End If
Return count
End Sub
Private Function decreaseCount(ByRef count as integer) As Integer
count -= 1
If count - 1 > ImageList1.Images.Count OR count < 0 Then
count = 0
End If
Return count
End Sub
e.Delta is how much you scrolled, this is depending on the options in the control panel about mouse wheel scrolling. So we can't be sure what these values will be, but the good thing is that scrolling ups is always positive and scrolling down is negative.

Related

If my counter ends at 10 at textbox1(TB1) the random number won't show in my textbox2(TB2)

If my counter ends at 10 at textbox1 (TB1) the random number won't show in my textbox2(TB2)
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TB1.Text = 10 Then
Dim num1 As Integer
Dim randomnumber As New Random
num1 = randomnumber.Next(100, 201)
TB2.Text = num1
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TB1.Text = TB1.Text + 1
If TB1.Text = 10 Then
Timer1.Enabled = False
End If
End Sub
End Class
Use a numeric variable to keep the count, not the TextBox. The TextBox.Text property is of type String, not Integer!
Putting Option Strict On at the top of your code will enforce using proper types. VB by default allows you to loosely convert between integer and string, which is poor practice.
Option Strict On
Public Class Form1
Private count As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
count = 0
Timer1.Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If count = 10 Then
Dim randomnumber As New Random()
Dim num1 = randomnumber.Next(100, 201)
TB2.Text = num1.ToString()
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
count += 1
Timer1.Enabled = count <> 10
TB1.Text = count.ToString()
End Sub
End Class

Background image moving vb.net

I'm trying to create a small game, but I'm getting trouble.
I would like the background image to move as a loop when the player is moving across the map. The following code is a sample of what I want but the reverse.
Public Class Form1
Private WithEvents Tmr As New Timer With {.Interval = 40}
Private water As New Bitmap("C:\TestFolder\Water.png")
Private waterposition As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Size = New Size(240, 74)
Tmr.Start()
End Sub
Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
waterposition += 1
If waterposition = water.Width Then waterposition = 0
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(water, waterposition, 0)
If waterposition > 0 Then e.Graphics.DrawImage(water, 0 - (water.Width - waterposition), 0)
End Sub End class
And now, when I'm trying to reverse it (from right to left) The image won't redraw.
Private Sub Tmr_Tick(sender As Object, e As EventArgs) Handles Tmr.Tick
waterposition -= 1
If waterposition = 0 Then waterposition = water.Width
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(water, waterposition, 0)
If waterposition < 0 Then e.Graphics.DrawImage(water, (water.Width + waterposition), 0)
End Sub
Any suggestion? Thanks

How to auto repeat counter in label and call an event when reach maximum count.

I want to make a program that has a counter until 5 and when reach 5 it will auto click a button and must count back from 0 - 5 again and again every time reach 5.
below is my code in timer click.
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer2.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime2.Text = ts.ToString("ss")
Else
lblTime2.Text = "00"
Timer2.Stop()
btnRefresh.PerformClick()
End If
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
messagebox.show 'me dot click'
End Sub
End Class
here is my solution to my question:
when the timer tick i auto increment my label 1- 5 and when it reach 5 it will return to 0 and count again 1 - 5. and repeat. then can do something after reach 0. hope can help
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Label1.Text + 1
If Label1.Text = "5" Then
Label1.Text = 0
btnRefresh.PerformClick()
End If
End Sub

media player cannot play next song in listbox

If AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded Then
ListBox1.SelectedIndex += 1
AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
AxWindowsMediaPlayer1.Ctlcontrols.play()
End If
It changes the index of the listbox, but it doesn't play the next file.
The file exist and there's no problem playing it the normal way.
Does anybody know what's wrong?
Solution is must to add timer"
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Dim item As Integer
item = ListBox1.SelectedIndex
If Form1.AxWindowsMediaPlayer1.playState = WMPPlayState.wmppsStopped Then
Me.ListBox1.SelectedIndex = item + 1
Form1.AxWindowsMediaPlayer1.URL = FileUrls(ListBox1.SelectedIndex)
Timer1.Start()
Else
Timer1.Start()
End If
end sub
also the following code work . first add timer to playlist and past following code
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Form1.AxWindowsMediaPlayer1.playState = WMPPlayState.wmppsStopped Then
If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
form1.axwindowmediaplayer1.url=listbox1.selectedindex
Else
'do nothing if no item to play
End If
End If
End Sub

Formatting percentages more thorougly

I've been asked to make a program to find percentages of males, females and children, but I'm kind of stuck - it's hard to explain, but when you enter an age that's younger than or equal to 17 it would be a child and anything over would be a male or female. However when it's entered it counts it as two people instead of one because it asks for the gender after the age if it's younger than 17. If it's hard to explain, please let me know and I'll try to improve on how I'm telling it.
Current code:
Public Class Form1
Dim MalePercent, FemalePercent, ChildPercent As Single
Dim MaleCount, FemaleCount, ChildCount, CountTotal, Age As Integer
Dim Gender As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim buttonArray = {btnMale, btnFemale}
For Each button In buttonArray
button.Enabled = False
Next
MessageBox.Show("To start this program, please click a button of your choice.")
txtAge.Select()
txtAge.Clear()
End Sub
Private Sub btnMale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMale.Click
MaleCount += 1
CountTotal += 1
lblMaleCount.Text = MaleCount
lblCountTotal.Text = CountTotal
btnMale.Enabled = False
btnFemale.Enabled = False
txtAge.Enabled = True
txtAge.Select()
txtAge.Clear()
End Sub
Private Sub btnFemale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFemale.Click
FemaleCount += 1
CountTotal += 1
lblFemaleCount.Text = FemaleCount
lblCountTotal.Text = CountTotal
btnFemale.Enabled = False
btnMale.Enabled = False
txtAge.Enabled = True
txtAge.Select()
txtAge.Clear()
End Sub
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
MalePercent = MaleCount / CountTotal
lblMalePercent.Text = Format(MalePercent, "#0.00%")
FemalePercent = FemaleCount / CountTotal
lblFemalePercent.Text = Format(FemalePercent, "#0.00%")
ChildPercent = ChildCount / CountTotal
lblChildPercent.Text = Format(ChildPercent, "#0.00%")
End Sub
Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
MessageBox.Show("Thank-you for using this program. Good-bye!")
Close()
End Sub
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Age = txtAge.Text
If Age <= 17 Then
MessageBox.Show("Now please select the appropriate gender.")
ChildCount += 1
CountTotal += 1
lblChildCount.Text = ChildCount
lblCountTotal.Text = CountTotal
ElseIf Age >= 18 Then
MessageBox.Show("Now please select the appropriate gender.")
End If
txtAge.Enabled = False
Dim buttonArray = {btnMale, btnFemale}
For Each button In buttonArray
button.Enabled = True
Next
End Sub
Looks like you're incrementing CountTotal twice when it's a Child, once then the age is entered and again when a gender is chosen.
Try this:
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Age = txtAge.Text
If Age <= 17 Then
ChildCount += 1
'CountTotal += 1 <= remove this
lblChildCount.Text = ChildCount
'lblCountTotal.Text = CountTotal <= remove this
End If
'messagebox here because you always ask for gender no matter the age
MessageBox.Show("Now please select the appropriate gender.")
txtAge.Enabled = False
Dim buttonArray = {btnMale, btnFemale}
For Each button In buttonArray
button.Enabled = True
Next
End Sub