VB Stopwatch Controlled by Mouse - vb.net

Im making a stop watch to measure how long it takes for certain weapons to empty their clip in CS:GO. I have the stopwatch programmed, but it only seems to work if im clicking within the bounds of the window. If its possible, does anyone know how to make it able to start/stop even while in another program? Ill post the code below.
Public Class Form1
Dim WithEvents timer As New Timer
Dim milliseconds As Integer
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
Timer1.Start()
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
Timer1.Stop()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
totalTime.Text = 0.0
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
totalTime.Text = totalTime.Text + 0.01
End Sub
End Class

The MouseDown and MouseUp events only happen when the mouse clicks within your application so you can't use them as events to track the time the mouse is held down in another application.
You may be able to do what you want using a global mouse hook or by looking at windows messages such as the Mouse Down and Mouse Up messages

Related

Is there a Form event like "Shown" but runs the code every time it is displayed instead of the just first time?

Just noticed in one of my programs that the form event Shown only runs the code when the form is first displayed. Every other time after that it does not run the code. Is there a form event that does what shown does, but every time it is displayed instead of just the first? Or is there a way to get around it? This is the exact same for the Load event too.
Thanks heaps, appreciate any response.
I just tested with this in Form1:
Private f2 As New Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
f2.Show()
End Sub
and this in Form2:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Form2_Load")
End Sub
Private Sub Form2_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
If Visible Then
MessageBox.Show("Form2_VisibleChanged")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Hide()
End Sub
and it worked exactly as expected. Every time I clicked Button1 in Form1, Form2 was displayed. The first time I saw messages for "Form2_Load"and "Form2_VisibleChanged" and on subsequent occasions on for "Form2_VisibleChanged".

Custom tray panel in VB.NET

i need to create system tray "panel" like Windows Media Player.
(not icon only, but complette form with buttons, images, etc.)
Here is wmp screenshot:
Is it possible in VB.NET & Win 10?
Thanks and sorry for my english.. :)
You actually can do kind of a "tray panel", and it isn't quite difficult. Just create a Form Object and set its FormBorderStyle property to None, which will allow you to create your custom border. Then, do the following:
Public Class Form1
Public Timer1 As New Timer
Private Sub Form1_Load(sender as Object, e as Eventargs) Handles MyBase.Load
Timer1.Interval = 1
End Sub
Private Sub Form1_MouseDown(sender as Object, e as MouseEventargs)
Timer1.Start()
End Sub
Private Sub Form1_MouseUp(sender as Object, e as MouseEventargs)
Timer1.Stop()
End Sub
Private Sub Timer1_Tick(sender as Object, e as Eventargs)
Me.Location = New Point(Me.Cursor.Position.X - (Me.Cursor.Position.X - Me.Location.X), Me.Cursor.Position.Y - (Me.Cursor.Position.Y - Me.Location.Y))
End Sub
End Class
Once you've done that (I'm not sure it will work directly, try a bit and it should), enjoy designing the GUI... ;-)
Hope this helps and by the way, your english is better than you think!

How do i pass keyUp/keyDown events to panel in visual basic?

I have created a game in visual basic, and have decided to expand by adding multiple rooms on the game. Each room will be a panel that shows when the user enters a certain area. To start, I created a class that inherits panel properties, then added it to my main Form. I then added all the items to the panel controls. My issue is that i cannot control the player when it is in the panel controls. I have tried all sorts, but couldn't get it to work.
I did some reading and found that panel does not come with keyUp() and keyDown() events. However, I would like to know if there is a way round this.
Thanks in advance!
Code of mainRoom() class (my panel):
Public Class mainRoom
Inherits Panel
Sub New()
With Me
.SetBounds(0, 0, Form1.Width, Form1.Height)
.SendToBack()
End With
End Sub
Public Sub mainRoom_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint
e.Graphics.FillRectangle(Brushes.DarkOrange, block)
End Sub
End Class
In main form class:
Sub setMap()
main = New mainRoom()
Me.Controls.Add(main)
End Sub
You can set the KeyPreview=True on the main form, and then handle the key up/down events from the form:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
'key down code.
e.Handled = True
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
'key up code.
e.Handled = True
End Sub
Note: if you don't want other controls' key up/down event handlers to fire after your form event handlers do, then set e.Handled=True as I have in the above example.
See MSDN for more info on KeyPreview.
BEWARE - when moving (cut/paste) existing controls via the IDE from a form into a new panel control, the event handlers are not carried across as they become children of the parent control (the new panel). So this happens:
Private Sub Button1_Click(sender As Object, e As EventArgs)
Add the event handlers back and don't spend an age googling why keypress events suddenly stop firing in panels ...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Continuous update feature/event

Is there some kind of update feature like in C# for Visual Studio 2012 in vb.net? (so that it will continuously check to see if an if statement is fulfilled)
Try something like this:
Dim iClicks As Integer = 0
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
iClicks += 1
Label1.Text = iClicks.ToString()
If iClicks = 1000 Then
MessageBox.Show("1000 reached!")
End If
End Sub
It's better to have a integer counter than checking the string value and performing conversion each time. As you can see, the code checks the click counter each time you click the label.
If you want to check the value periodically, add a Timer and perform the check in its event:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If iClicks = 1000 Then
MessageBox.Show("1000 reached!")
End If
End Sub
But consider the need to do this, because maybe it's not necessary and will decrease the performance comparing it with the other way.
There is probably a better way of doing this, but what I've done when I need a constant "update" function, is just use a Timer, then handle the Ticks.
Easiest way of implementing it is to go to the form designer, in the Toolbox, under Components, drag a timer onto the form. Double-click it to add a handle for its Tick event.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If WhateverYoureLookingFor = True
'Do stuff
End If
End Sub
This is obviously VB, but easy to convert to C#.

Mouse click and hold event?

Does anyone have an idea how to handle a mouse click and hold event in VB.NET?
Suppose I want to click and hold in a button and do some stuff behind the code like change the button BackColor or close a window faster( just like when you click in the ms office 2013 file menu then use a left-arrow to close that menu).
Hope you know what I mean
Thank you
You could create a timer that is defined globally that begins when MouseDown is called, then ends on Mouse Up. You can then set a condition on how many milliseconds need to pass before you deem it a 'long click'. See example code below:
Public Class Form1
Dim WithEvents timer As New Timer
Dim milliseconds As Integer
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
timer.Start()
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
timer.Stop()
Label1.Text = "Button held down for: " & milliseconds & " milliseconds"
If milliseconds >= 10 then 'Mouse has been down for one second
DoSomething()
End If
End Sub
Private Sub EggTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick
milliseconds += 1
End Sub
End Class
MouseDown is what you are looking for