I need to calculate the discount amount of all the values in another form's ListBox, then print that value in both the discount form and the main form.
Discount form code:
Private Sub btnCalculateVisit_Click(sender As Object, e As EventArgs) Handles btnCalculateVisit.Click
Dim dblVisits As Double = 0
If (IsNumeric(txtVisits.Text)) Then
If txtVisits.Text >= 0 And txtVisits.Text <= 3 Then
dblVisits = (dblVisitRates(0) * GlobalVariables.price)
ElseIf txtVisits.Text >= 4 And txtVisits.Text <= 8 Then
dblVisits = (dblVisitRates(1) * GlobalVariables.price)
ElseIf txtVisits.Text >= 9 And txtVisits.Text <= 13 Then
dblVisits = (dblVisitRates(2) * GlobalVariables.price)
ElseIf txtVisits.Text >= 14 And txtVisits.Text <= 1000000 Then
dblVisits = (dblVisitRates(3) * GlobalVariables.price)
End If
End If
lblDiscountAmount.Text = "$" + dblVisits.ToString
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Dim main As New MainForm()
main.lblDisRate.Text = GlobalVariables.dblType.ToString
Me.Close()
End Sub
The calculation prints out on the discount form, but doesn't change the label in the main form. How do i do this?
try it with the caption property and do a refresh to the form.
lblDiscountAmount.Caption = "$" + dblVisits.ToString
Me.refresh
Related
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
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
Public Shared total1 As Integer = 0
Public Shared total2 As Integer = 0
Private Sub BtnAdult_Click(ByVal sendenr As System.Object, ByVal e As System.EventArgs) Handles BtnAdult.Click
Dim addADult As Integer = 50
total1 = Val(total1) + Val(addADult)
total2 = Val(total2) + Val(addADult)
If RadioButton1.Checked Then
Sales.lblErick.Text = Val(total1)
Sales.Show()
Me.Hide()
ElseIf RadioButton2.Checked Then
Sales.lblHenry.Text = Val(total2)
Sales.Show()
Me.Hide()
End If
End Sub
Private Sub BtnChild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnChild.Click
Static hits As Integer = 0
hits += 1
Dim addChild As Integer = 45
total1 = Val(total1) + Val(addChild)
total2 = Val(total2) + Val(addChild)
If RadioButton1.Checked Then
Sales.lblErick.Text = Val(total1)
Sales.Show()
Me.Hide()
ElseIf RadioButton2.Checked Then
Sales.lblHenry.Text = Val(total2)
Sales.Show()
Me.Hide()
'...
ok i have two radio buttons
then i have two label
when i checked 1 radiobutton, then tick the btnAdult
i want my label to have a value of 0+ 50
and when i went back again to check the other button
it just adds to the value of the other label ..
supposedly
if checked the other radiobutton and tick btnAdult
my other label.text becomes 100,
it supposed to start with 50
what should i do...
It's because you call this code on every click
total1 = Val(total1) + Val(addADult)
total2 = Val(total2) + Val(addADult)
Means, every time you press the button totals will go +50
If you want a fix, then make it like this:
Dim addADult As Integer = 50
If RadioButton1.Checked Then
total1 = Val(total1) + Val(addADult)
Sales.lblErick.Text = Val(total1)
Sales.Show()
Me.Hide()
ElseIf RadioButton2.Checked Then
total2 = Val(total2) + Val(addADult)
Sales.lblHenry.Text = Val(total2)
Sales.Show()
Me.Hide()
End If
End Sub
Or place the total's math inside the radiobutton's brackets
The code below runs and keeps repeating over and over again. I only pasted the up_timer but others do the same. Any ideas on how to make it run once, then repeat to the random loop and so on?
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Application.DoEvents()
Randomize()
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
If value = 1 Then
MsgBox("Up")
up.Start()
ElseIf value = 2 Then
MsgBox("Down")
down.Start()
ElseIf value = 3 Then
MsgBox("Left")
left.Start()
ElseIf value = 4 Then
MsgBox("Right")
right.Start()
End If
Timer1.Stop()
End Sub
Private Sub up_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles up.Tick
Static moveCount As Integer = 1
If Me.mob2.Location.Y > 12 Then
Me.mob2.Location = New Point(Me.mob2.Location.X, Me.mob2.Location.Y - 5)
End If
moveCount += 1
If moveCount = 10 Then
moveCount = 1
Me.Timer1.Start()
Me.up.Stop()
End If
End Sub
Looks like this is what you need, based on your complaint that timer1 repeats same numbers
Dim rand1 As New Random(CInt(Date.Now.Ticks And &h0000FFFF))
Dim value As Integer = rand1.Next(1, 4)
This will not repeat
Plus, you have to move Timer1.Stop() to beginning of the method
My code needs a little work
Public Class Form1
Dim Bread, TotalPrice As Double
Private Sub txtBread_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBread.TextChanged
If txtBread.Text = "" Then
TotalPrice = TotalPrice - Bread
lblBread.Text = Bread.ToString
lblPrice.Text = TotalPrice.ToString
Else
Bread = Val(txtBread.Text) * 3.25
lblBread.Text = Bread.ToString
TotalPrice = TotalPrice + Bread
lblPrice.Text = TotalPrice.ToString
End If
End Sub
End Class
My textbox is good for a one-digit number only.
So my error here is when i input two-digit numbers on my text box, it actually updates my labels, but when i press backspace it does not update anymore.
The value of the variable TotalPrice grows with each new input (no matter if it is bigger or smaller than the previous one) and thus the value of lblPrice.Text. For example:
txtBread.Text TotalPrice
1 1
15 16
1 17
If you explain what you want to accomplish exactly, I can update your code.
Dim Bread As Double
Dim TotalPrice as Double = 5 'Any constant value you want
Private Sub txtBread_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBread.TextChanged
If txtBread.Text = "" Then
lblBread.Text = Bread.ToString
lblPrice.Text = Convert.ToString(TotalPrice - Bread)
Else
Bread = Val(txtBread.Text) * 3.25
lblBread.Text = Bread.ToString
lblPrice.Text = Convert.ToString(TotalPrice + Bread)
End If
End Sub
Try with my sample ..
Public Class Form1
Dim Bread As Double
Dim TotalPrice As Double = 100 '---> maybe this is a result from a function
Private Sub txtBread_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBread.TextChanged
If txtBread.Text = "" Then
Bread = 0
Else
Bread = Val(txtBread.Text) * 3.25
End If
lblBread.Text = Bread.ToString
lblPrice.Text = (TotalPrice + Bread).ToString
End Sub
End Class