add keypress code to VB.Net - 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

Related

Dynamic Countdown Timer

I have a program that uses countdown timers, it works but I am trying to do something new with this blender timer. It uses a NumericUpDown and is supposed to add 5 minutes for each value, 1 being 5 minutes and 2 being 10 minutes etc. I did find a way adding 5 minutes to BlenderCountDownFrom but when pressing the BlenderButton to stop it, it kept the new value. I am a complete novice and don't know what I am doing so the code is probably horrible! The actual countdown code I got from an example on the internet.
BlenderButton is both the start and stop for the countdown. It will use a "Set" button to program the amount of minutes from the NumericUpDown.
Dim BlenderCountDownFrom As New TimeSpan(0, 5, 1)
Dim BlenderStopwatch As New Stopwatch
Private Sub BlenderButton_Click(sender As Object, e As EventArgs) Handles BlenderButton.Click
If BlenderButton.Text = "Start" Then
BlenderTimer.Interval = 100
BlenderButton.Text = "Stop"
BlenderTimer.Start()
BlenderStopwatch.Reset()
BlenderStopwatch.Start()
Else
BlenderButton.Text = "Start"
My.Computer.Audio.Stop()
BlenderTimerLabel.BackColor = Color.White
BlenderTimer.Stop()
BlenderStopwatch.Stop()
BlenderTimerLabel.Text = ("00:05:00")
Me.MiscTab.Text = "Misc"
End If
End Sub
Private Sub BlenderTimer_Tick(sender As Object, e As EventArgs) Handles BlenderTimer.Tick
If BlenderStopwatch.Elapsed <= BlenderCountDownFrom Then
Dim toGo As TimeSpan = BlenderCountDownFrom - BlenderStopwatch.Elapsed
BlenderTimerLabel.Text = String.Format("{0:00}:{1:00}:{2:00}", toGo.Hours, toGo.Minutes, toGo.Seconds)
Else
BlenderTimer.Stop()
My.Computer.Audio.Play(My.Resources.Alarm.Blender, AudioPlayMode.BackgroundLoop)
Me.MiscTab.Text = "Alarm"
BlenderStopwatch.Stop()
BlenderTimerLabel.BackColor = Color.Red
End If
End Sub
Private Sub BlenderSetButton_Click(sender As Object, e As EventArgs) Handles BlenderSetButton.Click
If BlenderUpDown.Value = 1 Then
BlenderTimerLabel.Text = ("00:05:00")
End If
If BlenderUpDown.Value = 2 Then
BlenderTimerLabel.Text = ("00:10:00")
'This below doesn't work
Dim BlenderCountDownFrom As New TimeSpan(0, 10, 1)
End If
If BlenderUpDown.Value = 3 Then
BlenderTimerLabel.Text = ("00:15:00")
End If
If BlenderUpDown.Value = 4 Then
BlenderTimerLabel.Text = ("00:20:00")
End If
If BlenderUpDown.Value = 5 Then
BlenderTimerLabel.Text = ("00:25:00")
End If
End Sub

Change value of a label in another form

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

Label text remains unchanged

So I'm deliberately trying to make a Download ETA Calculator using Visual Basic 2013 Ultimate.. but after 3 hours of work, I tried running it but it won't show the final result (The Estimated Time). I am also asking you how to convert the result into Time format..
here is the code
Public Class Form1
Private Property z As Object
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As
LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Process.Start("www.speedtest.net")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
Dim x, y, z, a As Integer
Dim sum
x = TextBox1.Text
y = TextBox2.Text
sum = Label1
If RadioButton1.Checked = True And RadioButton3.Checked = True Then
z = x / 1024 / y
a = z / 60
sum = a / 60
End If
If RadioButton1.Checked = True And RadioButton4.Checked = True Then
z = x / 1024 / y
a = z / 60 * 1024
sum = a / 60
End If
If RadioButton2.Checked = True And RadioButton3.Checked = True Then
z = x / 1048576 / y
a = z / 60
sum = a / 60
End If
If RadioButton2.Checked = True And RadioButton4.Checked = True Then
z = x / 1048576 / y
a = z / 60 * 1024
sum = a / 60
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs)
Handles
RadioButton1.CheckedChanged
End Sub
End Class
Error 2
http://tny.cz/7b5711f7
Error 3
http://tny.cz/09bf1f96
Dim sum
sum = Label1
Presumably Label1 is a label control so you're setting a variable to be equal to the label control.
Later on, you're trying to set this variable of type label control to a decimal
sum = a / 60
I would say you want probably want to replace all your
sum = a / 60
with something like
label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
and forget about variable sum all together

Run Random event once?

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

Timer to count down in VB.Net 2010?

I'm trying to use a timer to count down from a specified time I choose with the time being separated into minutes and seconds using the format MM:SS and then stop when the time reaches 00:00.
So far I've used a previous answer that was found on here and modified it to the best of my knowledge with counting down although I've hit a snag in which when the timer successfully starts counting down, it's delayed and out of sync when counting down the minutes.
For example, counting down from 120 seconds;
02:00 >
02:59 >
02:58 >
02:57 >
02:56 >
02:55
And then when continuing to count down past 90 seconds under the same test;
02:30 >
01:29 >
01:28 >
01:27 >
01:26 >
01:25
When the countdown reaches 00 or 30 seconds, it incorrectly displays the minutes left and can't understand or figure out how to fix it.
Here is my code for my Counting Timer;
Private Sub tmrCountdown_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles tmrCountdown.Tick
SetTime = SetTime - 1
lblTime.Text = FormatTime(SetTime)
If SetTime = 0 Then
tmrCountdown.Enabled = False
End If
End Sub
Here is my code for the Function formatting the time;
Public Function FormatTime(ByVal Time As Integer) As String
Dim Min As Integer
Dim Sec As Integer
'Minutes
Min = ((Time - Sec) / 60) Mod 60
'Seconds
Sec = Time Mod 60
Return Format(Min, "00") & ":" & Format(Sec, "00")
End Function
And here is my code for the Form Load;
Private Sub frmSinglePlayer_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
'Setting the time.
SetTime = 120
lblTime.Text = FormatTime(SetTime)
tmrCountdown.Enabled = True
End Sub
I've set;
Dim SetTime As Integer
At the top of my Public Class so I am able to input a specified time into the countdown timer. This is probably something incredibly silly and I can't figure out what it is.
Any help is greatly appreciated and please bare in mind, I am a beginner at programming and get easily confused with large walls of code. (I can barely understand the Function as it is.)
Thank you for helping!
Play with this:
Public Class frmSinglePlayer
Private TargetDT As DateTime
Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmrCountdown.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
tmrCountdown.Start()
End Sub
Private Sub tmrCountdown_Tick(sender As Object, e As System.EventArgs) Handles tmrCountdown.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime.Text = ts.ToString("mm\:ss")
Else
lblTime.Text = "00:00"
tmrCountdown.Stop()
MessageBox.Show("Done")
End If
End Sub
End Class
Take a tested sample of countdown timer. Make the changes you need(ex the format of the time).
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
SetTime = 70
AddHandler dtTimer.Tick, AddressOf dtTimer_Tick
dtTimer.Interval = New TimeSpan(0, 0, 1)
dtTimer.Start()
End Sub
Private Property SetTime As Integer
Private Sub dtTimer_Tick(sender As Object, e As EventArgs)
Dim iMinutes As Integer
Dim iSeconds As Integer
If SetTime = 0 Then
dtTimer.Stop()
txtTime.Text = "0:0"
Exit Sub
End If
SetTime -= 1
iMinutes = Math.Floor(SetTime / 60)
iSeconds = SetTime Mod 60
txtTime.Text = iMinutes & ":" & iSeconds
End Sub
Try this
'the amount of time to countdown from
Dim countDownFrom As New TimeSpan(0, 0, 10) 'ten seconds
'a Stopwatch to track how long running
Dim stpw As New Stopwatch
Private Sub Button1_Click(sender As Object, _
e As EventArgs) Handles Button1.Click
Timer1.Interval = 250 'how often to update display
Timer1.Start() 'start the display updater
stpw.Reset() 'restart the stopwatch
stpw.Start()
'or depending on version of .Net
'stpw.Restart
End Sub
Private Sub Timer1_Tick(sender As Object, _
e As EventArgs) Handles Timer1.Tick
If stpw.Elapsed <= countDownFrom Then
Dim toGo As TimeSpan = countDownFrom - stpw.Elapsed
lblTime.Text = String.Format("{0:00}:{1:00}:{2:00}", toGo.Hours, toGo.Minutes, toGo.Seconds)
Else
Timer1.Stop()
stpw.Stop()
End If
End Sub
your mistake in your original code is that you are using the MOD operater incorrectly.
'Minutes
Min = ((Time - Sec) / 60) Mod 60
'Seconds
Sec = Time Mod 60
At 2:00 you see 2:00 because:
Min = ((120-00) / 60 ) MOD 60 = 2 MOD 60 = 2
Sec = 120 MOD 60 = 0
At 1:59 you see 2:59 because
Min = (119 / 60) MOD 60 = 1.98 MOD 60 = 1.98 = 2
Sec = 119 MOD 60 = 59
After 31 seconds your minutes changes from 2 to 1 because
Min = (89 / 60) MOD 60 = 1.48 MOD 60 = 1.48 = 1
More simply:
Format((Math.Floor(lSeconds / 60)), "00") & ":" & Format((lSeconds Mod 60), "00")
Public SetTime As Integer = 0
Public Min As Integer = 0
Public Sec As Integer = 0
Public Decimaal As Decimal = 0
Public Function FormatTime(ByVal Time As Integer) As String
'Minutes
Min = Fix(SetTime / 60000)
'Decimaal
Decimaal = (SetTime / 60000) - Min
'Seconden
Sec = Fix(Decimaal * 60)
Return Format(Min, "00") & ":" & Format(Sec, "00")
End Function
Private Sub tmrCountdown_Tick(sender As Object, e As EventArgs) Handles tmrCountdown.Tick
SetTime = SetTime - 1000
lblCountdown.Text = FormatTime(SetTime)
tmrCountdown.Enabled = True
If SetTime = 0 Then
tmrCountdown.Enabled = False
End If
End Sub
You could just do
For i = 120 to 0 step -1
Console.writeline(i)
Next
But be aware it will instantly put them on the screen and it won't do it 1 at a time