Associate / Relate two numericupdown buttons - vb.net

I am trying to create a program in VB .NET which two numericupdown buttons should be associated, if value in samp button is changed max should also be controlled and vice a versa. I have written code for it but somehow it is not working perfectly. Please let me what I am missing here.
samp Minimum is set to 50 and Maximum 400; while
max Minimum is set is 0 and Maximum 350
Private Sub samp_changed() Handles config_samp.ValueChanged
If config_samp.Value <= 400 And config_samp.Value >= 299 Then
config_max.Value = 150
End If
If config_samp.Value <= 300 And config_samp.Value >= 199 Then
config_max.Value = 250
End If
If config_samp.Value <= 198 And config_samp.Value >= 50 Then
config_max.Value = 350
End If
End Sub
Private Sub max_changed() Handles config_max.ValueChanged
If config_max.Value <= 350 And config_max.Value >= 251 Then
config_samp.Value = 200
End If
If config_max.Value <= 250 And config_max.Value >= 151 Then
config_samp.Value = 300
End If
If config_max.Value <= 150 And config_max.Value >= 101 Then
config_samp.Value = 400
End If
End Sub

When the program executes samp_changed(), the .Value of config_max is changed, so it calls max_changed(), which might change the value of config_samp, which would mean that samp_changed() is called, and it gets locked in to one value eventually.
What you need to do is temporarily stop each method from causing the other to be called. You can do that by explicitly removing the handler, changing the value, and adding the handler.
Those If config_max.Value <= 350 And config_max.Value >= 251 Then, and so on, lines are a bit unwieldy. It would be easier to see what is going on by using a Case statement, something like this:
Public Class Form1
Private Sub samp_changed(sender As Object, e As EventArgs)
Dim newMaxValue = 0
Dim valueChanged = False
Select Case config_samp.Value
Case 50 To 198
newMaxValue = 350
valueChanged = True
Case 199 To 300
newMaxValue = 250
valueChanged = True
Case 299 To 400
newMaxValue = 150
valueChanged = True
End Select
If valueChanged Then
RemoveHandler config_max.ValueChanged, AddressOf max_changed
config_max.Value = newMaxValue
AddHandler config_max.ValueChanged, AddressOf max_changed
End If
End Sub
Private Sub max_changed(sender As Object, e As EventArgs)
Dim newSampValue = 0
Dim valueChanged = False
Select Case config_max.Value
Case 101 To 150
newSampValue = 400
valueChanged = True
Case 151 To 250
newSampValue = 300
valueChanged = True
Case 251 To 350
newSampValue = 200
valueChanged = True
End Select
If valueChanged Then
RemoveHandler config_samp.ValueChanged, AddressOf samp_changed
config_samp.Value = newSampValue
AddHandler config_samp.ValueChanged, AddressOf samp_changed
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler config_samp.ValueChanged, AddressOf samp_changed
AddHandler config_max.ValueChanged, AddressOf max_changed
End Sub
End Class

Related

How can I make a timer slower and slower until it stops?

Can someone tell me what's wrong or what I need to add to make it run the way that I want? I need to make it slower, then slower again, then make it stop.
Heres what I have tried:
Dim A As Integer
If Timer.Interval = 1 Then
A = Timer.Interval + 1000
If Timer.Interval = 1000 Then
Timer.Enabled = False
End If
End If
I want to stop the timer after a specific time but I have button 1 and 2 its random and stop, if I push the button random it will select any on given list then if I push stop the selection of random is going stop delay like spinning wheel, the missing on my program is the delay selection after a seconds its like selecting random then it will slowly selecting then it will stop.
Here's the btnRandom.Click event handler:
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
If lstList1.Items.Count <= 1 Then
MessageBox.Show("Name must more than 1 to run the randomizer.", "Error Loading", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
txtName.Enabled = False
btnRandom.Enabled = False
btnAdd.Enabled = False
btnRemove.Enabled = False
btnStop.Enabled = True
btnAdd.ForeColor = Color.FromArgb(64, 64, 64)
btnRemove.ForeColor = Color.FromArgb(64, 64, 64)
btnRandom.ForeColor = Color.FromArgb(64, 64, 64)
If btnRandom.Enabled = False Then
Timer2.Start()
lstList1.ClearSelected()
End If
End If
End Sub
And the Timer.Tick handler:
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If btnRandom.Enabled = True Then
Timer2.Enabled = False
End If
If btnRandom.Enabled = False Then
txtNameLabel.Text = lstList1.Items(Rnd() * (lstList1.Items.Count - 1))
lstList1.SelectedItem = txtNameLabel.Text
txtNameLabel.Text = lstList1.SelectedItem
End If
End Sub
If you want something to decelerate then you need to multiply the speed by a constant less than one, which in this case is equivalent to increasing the display time interval by a constant greater than one.
So, you can start something at one speed, and then add a deceleration by changing the amount the interval is changed by.
Something like this:
Public Class Form1
Dim sw As Stopwatch
Dim tim As Timer
Dim intervalMultiplier As Double = 1.0
Dim itemsToDisplay As List(Of String)
Dim itemIndex As Integer = 0
Private Sub tim_Tick(sender As Object, e As EventArgs)
' When intervalMultiplier > 1.0 the time between ticks will increase.
tim.Interval = CInt(tim.Interval * intervalMultiplier)
Label1.Text = itemsToDisplay(itemIndex)
If tim.Interval >= 1000 Then
tim.Stop()
End If
itemIndex = (itemIndex + 1) Mod itemsToDisplay.Count
End Sub
Private Sub StartTimer()
If tim Is Nothing Then
tim = New Timer()
AddHandler tim.Tick, AddressOf tim_Tick
End If
If sw Is Nothing Then
sw = Stopwatch.StartNew()
Else
sw.Restart()
End If
intervalMultiplier = 1.0
tim.Interval = 100
tim.Start()
End Sub
Private Sub bnStop_Click(sender As Object, e As EventArgs) Handles bnStop.Click
intervalMultiplier = 1.25
End Sub
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
' Your code to disable buttons etc. goes here.
StartTimer()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set up some data for this example.
itemsToDisplay = New List(Of String) From {"Capacitor", "Diode", "Inductor", "Resistor", "Transistor"}
End Sub
End Class

How can I use collision detection with spawned arrays

Since I have been trying to make a space invaders style game I have been having trouble with collision detection with spawned objects in arrays (and having a bit of trouble with the bullets, they keep stopping and having another generate). I am new at coding and would like some help with these issues, or at least some links to some forums that had the same thread question.
here is my code:
Public Class Form1
'global variables
Dim intAmountOfEnemys As Short = 9
Dim intRowsOfEnemys As Integer = 0 '**
Dim intAmountOfBullets As Integer = 0
Dim picEnemysWave1(intAmountOfEnemys) As PictureBox
Dim lblBullets As New Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Welcome_Screen.Hide()
Call EnemyWaves(picEnemysWave1)
End Sub
Sub PlayerMovement(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.A Then
If picShip.Right <= 0 Then
picShip.Left = 1567
Else
picShip.Left -= 10
End If
ElseIf e.KeyCode = Keys.D Then
If picShip.Left >= 1567 Then
picShip.Left = -15
Else
picShip.Left += 10
End If
ElseIf e.KeyCode = Keys.Space Then
Do
BulletGeneration(lblBullets)
Loop Until Keys.Space
lblBullets.Left = (picShip.Left + 7)
End If
End Sub
#Region "Enemy waves, Movement, and Properties"
Sub EnemyWaves(ByRef picEnemysWave1() As PictureBox)
'Enemy Generator
Const srtENEMYSPACING_Y As Short = 155
For intCounterForEnemys As Integer = 0 To intAmountOfEnemys
Dim intEnemySpacing As Integer = srtENEMYSPACING_Y * intCounterForEnemys
picEnemysWave1(intCounterForEnemys) = New PictureBox
picEnemysWave1(intCounterForEnemys).Location = New Point(42 + intEnemySpacing, 1)
picEnemysWave1(intCounterForEnemys).Image = My.Resources.enemy
picEnemysWave1(intCounterForEnemys).Width = 124
picEnemysWave1(intCounterForEnemys).Height = 84
picEnemysWave1(intCounterForEnemys).Show()
Me.Controls.Add(picEnemysWave1(intCounterForEnemys))
Next intCounterForEnemys
End Sub``
Private Sub TmrAlien1_Tick(sender As Object, e As EventArgs) Handles TmrAlien1.Tick
For intRandom As Integer = 0 To 9
picEnemysWave1(intRandom).Top += 3
Dim intRandomNum As Integer = Rnd()
If intRandomNum > 0.66 Then
picEnemysWave1(intRandom).Left += 2 'goes left randomly
ElseIf intRandomNum < 0.33 Then
picEnemysWave1(intRandom).Left -= 2 'goes right randomly
End If
If picEnemysWave1(intRandom).Top <= 0 Then
TmrAlien1.Start()
End If
If picEnemysWave1(intRandom).Top >= 952 Then
TmrAlien1.Stop()
End If
Next intRandom
End Sub
#End Region
#Region "Bullet Generation, Movement, and Properties"
Sub BulletGeneration(ByRef lblBullets As Object)
'Generation of Bullets
For intBulletCounter As Integer = 0 To intAmountOfBullets
lblBullets = New Label
lblBullets.location = New Point(760, 785)
lblBullets.image = My.Resources.blast2
lblBullets.width = 32
lblBullets.height = 64
lblBullets.show()
Me.Controls.Add(lblBullets)
Next intBulletCounter
End Sub
Private Sub tmrBullets_Tick(sender As Object, e As EventArgs) Handles tmrBullets.Tick
lblBullets.Top -= 20
End Sub
#End Region
#Region "Collision Detection"
Sub BulletCollision(ByRef lblBullets As Label, ByRef intAmontOfEnemys As Integer)
For Each picEnemy As PictureBox In picEnemysWave1
If lblBullets.Bounds.IntersectsWith(picEnemy.Bounds) Then
picEnemy.Location = New Point(3900, 8700)
Exit For
End If
Next
'what Im trying
End Sub
#End Region

add keypress code to VB.Net

I am newbie in vb.net and I am creating a code for my assignment. Please help me to add keypress code to it :( I created the interface and it runs perfectly without keypress code but then I added a keypress code, error comes out :(
All I want to do is to add a keypress code that when I press enter, the output of lblGreet is "Jolly Morning (insert person's name)" and when I press enter again the output of lblGreet is without the name of that person.
Here is my code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblGreet.Width = Me.Width
lblGreet.Top = (Me.Height / 2) - (lblGreet.Height / 2)
lblGreet.Left = (Me.Width / 2) - (lblGreet.Width / 2)
lblWelcome.Width = 800
lblDate.Top = 50
lblDate.Left = 50
lblTime.Top = 100
lblTime.Left = 50
Timer1.Enabled = True
Me.Location = New Point(0, 0)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim hour As Integer = DateTime.Now.Hour
lblDate.Text = Date.Now.ToLongDateString
lblTime.Text = Date.Now.ToLongTimeString
If hour >= 12 And hour < 18 Then
lblGreet.Text = "Jolly Afternoon!"
ElseIf hour < 12 And hour >= 0 Then
lblGreet.Text = "Jolly Morning!"
ElseIf hour >= 18 And hour <= 24 Then
lblGreet.Text = "Jolly Evening!"
End If
End Sub

Visual Basic How to reference a dynamically created button

My code creates 40 buttons dynamically I can change properties if the button is clicked using sender but how do I reference them when I click a different button
Public Sub loadButtons()
For i As Integer = 0 To 39
If i > 19 Then
gap = i + 10
Else
gap = i
End If
Dim B As New Button
Form1.panSeats.Controls.Add(B)
B.Height = 30
B.Width = 37
B.Left = ((i Mod 10) * 47) + 322
B.Top = ((gap \ 10) * 31) + 114
B.Text = Chr((i \ 10) + Asc("A")) & i Mod 10 + 1
B.Tag = i
Buttons.Add(B.Text, B)
If isBooked(i) = True Then
B.BackColor = Color.Red
Else
B.BackColor = Color.Orange
End If
AddHandler B.Click, AddressOf Form1.Button_Click
Next
End Sub
I want to be able to change the backcolor B isn't declared have tried using the button.tag but not working
Private Sub btnTestCancel_Click(sender As Object, e As EventArgs) Handles btnTestCancel.Click
B.BackColor = Color.Orange
End Sub
The Addhandler statement wires the events for all dynamically added buttons. The sender object is the one being clicked, we just need to unbox it.
AddHandler B.Click, AddressOf btns_Click
Private Sub btns_Click(sender As Object, e As EventArgs) 'no handles clause needed
Dim btn As Button = DirectCast(sender, Button)
btn.BackColor = Color.Orange
End Sub

Collision detection in vb.net

I am trying to recreate Copter in visual basic, so far I have the player, roof and helicopter but I can't seem to get the game to end when the player touches the floor or roof. Sorry for posting so much, this is my first time on here and I didn't know what to paste. Any help is appreciated :D
Public Class Form1
Dim pb_field As PictureBox
Private Sub create_field()
pb_field = New PictureBox
With pb_field
.Top = 20
.Left = 20
.Width = 500
.Height = 300
.BackColor = Color.Black
End With
Me.Controls.Add(pb_field)
pb_field.BringToFront()
End Sub
Dim pb_player As PictureBox
Private Sub create_player()
pb_player = New PictureBox
With pb_player
.Width = 20
.Height = 20
.BackColor = Color.Red
.Top = pb_field.Top + pb_field.Bottom / 2
.Left = pb_field.Left + 20
End With
Me.Controls.Add(pb_player)
pb_player.BringToFront()
End Sub
#Region "Roof Stuff"
Dim roof(10000) As PictureBox
Dim num_of_roof As Integer = -1
Dim r As New Random
Private Sub create_roof()
num_of_roof += 1
roof(num_of_roof) = New PictureBox
With roof(num_of_roof)
.Top = pb_field.Top
.Left = pb_field.Right
.Height = r.Next(20, 40)
.Width = 20
.BackColor = Color.RoyalBlue
End With
Me.Controls.Add(roof(num_of_roof))
roof(num_of_roof).BringToFront()
End Sub
#End Region
#Region "floor Stuff"
Dim floor(10000) As PictureBox
Dim num_of_floor As Integer = -1
Private Sub create_floor()
num_of_floor += 1
floor(num_of_floor) = New PictureBox
With floor(num_of_floor)
.Left = pb_field.Right
.Height = r.Next(20, 40)
.Width = 20
.Top = pb_field.Bottom - floor(num_of_floor).Height
.BackColor = Color.YellowGreen
End With
Me.Controls.Add(floor(num_of_floor))
floor(num_of_floor).BringToFront()
End Sub
#End Region
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
Me.Text = e.KeyChar
If e.KeyChar = "w" Then
pb_player.Top -= 10
End If
**Dim collision As Boolean
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For
Else : collision = False
End If
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If**
Next
End Sub
Private Sub form1_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
create_field()
create_roof()
create_player()
tm_background.Start()
tm_gravity.Start()
End Sub
Private Sub tm_background_Tick(sender As Object, e As EventArgs) Handles tm_background.Tick
For i = 0 To num_of_roof
roof(i).Left -= 20
If roof(i).Left < pb_field.Left Then
Me.Controls.Remove(roof(i))
End If
Next
create_roof()
For i = 0 To num_of_floor
floor(i).Left -= 20
If floor(i).Left < pb_field.Left Then
Me.Controls.Remove(floor(i))
End If
Next
create_floor()
End Sub
Private Sub tm_gravity_Tick(sender As Object, e As EventArgs) Handles tm_gravity.Tick
pb_player.Top += 5
End Sub
This is the code I was attempting to use after looking online at possible solutions
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) HandlesMe.KeyPress
Me.Text = e.KeyChar
If e.KeyChar = "w" Then
pb_player.Top -= 10
End If
Dim collision As Boolean
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For
Else : collision = False
End If
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If
Next
End Sub
Your problem is where you exit the for loop before displaying the message:
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For ' Note that exiting skips your check after the End If below
Else : collision = False
End If
' Whenever this is true you have already exited your 'for' loop
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If
Next
Instead you need something like this where you evaluate the condition after the loop:
For Each PictureBox In Me.Controls
If pb_player.Bounds.IntersectsWith(roof(num_of_roof).Bounds) Then
collision = True
Exit For
Else : collision = False
End If
Next
If collision = True Then
MessageBox.Show("Unlucky,better luck next time!")
End If
First you need to set tag's in the floor and ceiling regions
With floor(num_of_floor)
.Tag = "boundaries"
Then you can refer to each picturebox in your controls
For Each box As PictureBox In Me.Controls
If box.Tag <> "boundaries" Then Continue For
If pb_player.Bounds.IntersectsWith(box.Bounds) Then
collision = True
Exit For
Else : collision = False
End If
Next
However, you are still going to have a problem that when it hits the floor it will not pass as a collision, because all this code is going on in the key press,
If a user lets the coptor fall, it will only lose the next time they click on the keyboard