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
Related
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
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.
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.
I have been doing some coding for a system and need to use threading instead of normal timers in VB.NET.
It works fine but the problem lies in the blink timings, when the button is clicked then it blinks as expected, if in testing it is clicked more than once then the blinking time roughly multiplies by the original sleep thread time (750ms), this continues to happen for every click.
What can I do to make the blink not speed up? Below is the code!
Private _flash As Boolean = False
Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click
_flash = True
Dim FlashThread As New Thread(New ThreadStart(AddressOf FlashLabel))
FlashThread.Start()
End Sub
Private Sub FlashLabel()
Dim _color As Color = Color.Gray
While _flash
If label1.ForeColor.Equals(_color) Then
label1.ForeColor = Color.Red
Else
label1.ForeColor = Color.Gray
System.Threading.Thread.Sleep(750)
End While
End Sub
Every time the button is clicked, you are starting a new thread, so if you click the button twice, it will start two threads, both of which are toggling the colors at 750 millisecond intervals, so it appears as if there was one thread doing it twice as fast. A simple way around this is to simply skip starting the new thread if the _flash flag is already set, for instance:
Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click
If Not _flash Then
_flash = True
Dim FlashThread As New Thread(New ThreadStart(AddressOf FlashLabel))
FlashThread.Start()
End If
End Sub
You never cancel the original thread, so when you click the button a second time you now have two threads running and causing the blink to happen.
So you can either cancel the thread in another action or only start the thread the first time and then set _flash to true and false after that.
I am doing project in vb.net
When i click on button open I opened form with no control box(minimize,maximize etc).set borderStyle to FixedToolWindow
I want to change the opacity of form on got focus & lost focus event.
I also used activated & deactivated event but doesnt working
Private Sub form_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs)HandlesMyBase.Deactivate
Me.Opacity =0
End Sub
Private Sub form_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Me.Opacity = 1
End Sub
To do this you need to use a System.Windows.Forms.Timer. the implementation is very simple:
Have two variables called _fromOpactity and _toOpacity, and a constant OpacityStep = 0.05
on Form Activate or Deactivate set _fromOpacity and _toOpacity and start the timer to fade in/out.
In the timer Elapsed event handler, Increment or Decrement OpacityStep (depending on from/to) until the desired value is reached.
For a full example of how to do this, see this article.
Best regards,
Try 0.01 in your 2nd line. You used 0 and it will hide your form.
Because that when you click in form area , the form_Actived don't run.