Visual Basic adding number loop - vb.net

I am trying to make an idle game something like cookie clicker, and I am having problems with making a simple line of code that repeats every second and adds 5 to a number every second. Could anyone help me? I want this to start the loop if someone clicks the button.

you could use a timer. Enable/start the timer when the button is clicked.
See example at MSDN: Windows Form Timer

add a timer and in the button code type :
TimerName.start
and add this in the timer code :
TimerName.interval = 1000
'replace TimerName with the name of timer you just added
'this will add 5 to number you want every second , interval of timer = 1000 that means it does the code every second
NumberThatYouWant += 5
'Replace NameThatYouWant with the number name that you want to add 5 to it every second

Yeah create a timer, then set the timer.interval to 1000 to tick each second, then create a sub for timer.tick and put the number you want to be increased in there and that should work.
EG.
Private Sub Timer1_Tick() Handles Timer1.Tick
variable += 5
End Sub
You have to change the interval in the properties window (bottom right)
Hope this helps!
Edit: I didn't include Timer1.start because the other answers said that. Don't forget to use it.

Setting the Timer
First, add a timer control to your form. Set the timer's interval value to '1000' (the timer's interval is measured in milliseconds). You should also enable your timer at run-time:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
So your code should look similar to this:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Interval = 1000
End Sub
Adding the value with the Timer
Now say the button's name is 'button1', we will now finish the code to add 5 to the button's text property every 1 second, like so:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Interval = 1000
Button1.Text += 5
End Sub
This code can also be written as "Button1.Text = Button1.Text + 5" I hope this helps clear up the ambiguity.

Related

display a message to a label in vb.net at specified times during the day

I would like to display a sentence in a text box at a 5 specific times during the day automatically. for example:
at 5:30 AM,
Textbox1.text = "breakfast"
at 7:30 AM
textbox1.text = "leave for school",
etc.
a timer can just start when the application is launched, although it needs to refer to the local time or some constant time as the program needs to output at the same time each day of the week without me having to change it manually.
The proper way to do it is something like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Set the interval at startup.
Timer1.Interval = CInt(GetNextNotificationTime().Subtract(Date.Now).TotalMilliseconds)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Set the interval at each notification.
Timer1.Interval = CInt(GetNextNotificationTime().Subtract(Date.Now).TotalMilliseconds)
'Not sure whether this is required when the Interval changes or not.
Timer1.Stop()
Timer1.Start()
'Do the work here.
End Sub
Private Function GetNextNotificationTime() As Date
'...
End Function
How you implement that GetNextNotificationTime method depends on how the notification times are stored. The Timer will then Tick only when a notification is due.
You can still do this with a Timer and you wouldn't have to do any math...
Every time the Timer raises a Tick event, you check the value of: System.DateTime.Today.Now.ToString("HH:mm"). If it is equal to your preset time, change the text in the TextBox

Creating Timer Countdown VB.Net?

i am creating a minigame where when the use clicks a button, it "attacks" the monster causing it to lose 5 hp, displayed using a progressbar. then at the same time the monster also attacks making the player lose hp. but the problem is these events happen at the exact same time, and i would like a 2 second interval between the events. ive been trying to get a timer event to work since this morning, but it just wouldnt work here is my code
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Tick = 0 Then
Timer1.Stop()
Timer1.Enabled = False
Else
Tick -= 1
End If
End Sub
and here is the attack button event
Private Sub btnAttack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttack.Click
PlayerAttacks(mhealth, attack)
textShiftUp(numlines)
HPBarsAlter(mhealth, health)
Tick = 2
Timer1.Start()
MonsterAttacks(health, mattack, CritRate)
HPBarsAlter(mhealth, health)
MobDeath(mhealth, MobNumber)
Timer1.Stop()
End Sub
please tell me if you need any more information thank you :)
Basically, move your monster attack to the timer
Private Sub btnAttack_Click(...)
btnAttack.Enabled = False ' disable more attacks
PlayerAttacks(mhealth, attack)
textShiftUp(numlines)
HPBarsAlter(mhealth, health)
MonsterTimer.Interval = 2000
MonsterTimer.Start()
End Sub
Private Sub MonsterTimer_Tick(...
' not sure what the old code was doing
MonsterTimer.Stop ' stop the attacks
MonsterAttacks(health, mattack, CritRate)
HPBarsAlter(mhealth, health)
MobDeath(mhealth, MobNumber)
btnAttack.Enabled = True ' allow more attacks
End Sub
EDIT
Added 2 lines to toggle the ability to attack while waiting.

Detect if mouse stop moving vb.net

I would like to stop the timer whenever a mouse stops moving inside a groupbox
As of now, I start the timer when the mouse hover at the groupbox and stops it when it leaves the group box.
Private Sub gbxMouseMap_MouseHover(sender As Object, e As System.EventArgs) Handles gbxMouseMap.MouseHover
Timer.Start()
End Sub
Private Sub gbxMouseMap_MouseLeave(sender As Object, e As System.EventArgs) Handles gbxMouseMap.MouseLeave
Timer.Stop()
End Sub
In the MouseMove event set a class varible named LastMoveTime to the current timer elapsed time. In the MouseHover event check to see if LastMoveTime has reached the timeout period, if so stop the timer.
I will get you started...
Private LastMoveTime As DateTime
Private MouseTimeoutMilliseconds as Integer = 500
'put inside hover
If LastMoveTime.AddMilliseconds(MouseTimeoutMilliseconds) < Now Then
Timer.Stop()
Else
Timer.Start()
End if
To prevent having to handle this for many controls you can rearrange things a bit and cache the information needed to know if cursor has moved and how long the idle time is, to do this you need a Point variable and a Date variable. The Timer needs to tick all the time. In addition, to balance the cursor Show/Hide calls you need a variable to keep track of its visibility state. Here is the complete code sample:
Private loc As Point, idle As Date, hidden As Boolean,
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If loc <> Cursor.Position Then
If hidden Then
Cursor.Show()
hidden = False
End If
loc = Cursor.Position
idle = Date.Now
ElseIf Not hidden AndAlso (Date.Now - idle).TotalSeconds > 3 Then
Cursor.Hide()
hidden = True
End If
End Sub
This Timer can tick each 1/2-1 seconds depending on how responsive you want it, the idle time is set to 3 seconds. The code should be easy to understand when you read it and give it some thought, if not ask

Refreshing every minutes

In a form, there is a label. It shows whether web service is connected or not at every minutes. How to code to repeat this process? Will I use the thread or timer? Please share me.
You will need a timer object in order to run the code every X minutes. Using a separate thread to check the web service only needs to be done if it will take a while to check and you want your form to remain responsive during this time.
Using a timer is very easy:
Private WithEvents timer As New System.Timers.Timer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set interval to 1 minute
timer.Interval = 60000
'Synchronize with current form, or else an error will occur when trying to
'update the UI from within the elapsed event
timer.SynchronizingObject = Me
End Sub
Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer.Elapsed
'Add code here to check if the web service is updated and update label
End Sub

VB.NET WebBrowser click on button

I am working at a small VB.NET project which autofill the fields on the Yahoo register page. Is there a way to click on "Check" button and see if the entered ID is OK or not?
Something like if the entered ID is OK then proceed further with filling the field, if not, try another ID and press "Check" button again.
The webbrowser control lets you access elements within the webpage and you can invoke methods on them, so something as simple as this will click the button:
webBrowser1.Document.All("yidHelperBtn").InvokeMember("click");
Add a timer to your application, with an interval of 1000 ms. Here is the code:
Dim CheckButton, yahooId As HtmlElement
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
yahooId = WebBrowser1.Document.GetElementById("yahooid")
CheckButton = WebBrowser1.Document.GetElementById("yidHelperBtn")
yahooId.InnerText = "testID" 'Replace testID by the ID you want
Timer1.Start() 'Starts the timer: within 1000 ms (1 s). It will execute the Timer1_Tick sub.
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
CheckButton.Focus() 'Give the check button the focus
SendKeys.Send("{ENTER}") 'Causes the validation of the check button
Timer1.Stop() 'Stops the timer
End Sub
I added a timer because the browser doesn't seem to validate the Enter key while in the WebBrowser1_DocumentCompleted method.
With this code, you can know if the id you entered is OK or not. It is not complete, but it's a good beginning, try to understand and adapt it for your needs.