My first VB program, Error - vb.net

I am working on a program for my brother, to show him what VB is. I had my friend help me here, but I now get an error when running on these lines.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(10)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Tick = 1000
**Timer1.Start()**
End Sub
the **'s arent there, I was trying to bold it.
It returns me with this error:
Error 1 'Public Event Tick(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. C:\Users\Kyle\Desktop\Form1.vb 10 9 WindowsApplication1

Tick is an Event of the timer. to set the interval use Interval property, or pass it to the constructor.
timer = New Timer(3000)
timer.Enabled = True

Related

VB.NET Timer stops working after it ticks the first time

the timer in my code stops working after the first tick, I have it set to start ticking every second when the form loads:
Private Sub FormIdleTimeWaster_Load(sender As Object, e As EventArgs) Handles MyBase.Load
timerCPS.Interval = 1000
timerCPS.Start()
End Sub
but after the first tick, it stops working:
Private Sub TimerCPS_Tick(sender As Object, e As EventArgs) Handles timerCPS.Tick
lblCPS.Text = CStr(CPS)
lblTotalHW.Text = CStr(CPS + HWTotal)
End Sub
All the other code refrencing timerCPS is
Me.timerCPS = New System.Windows.Forms.Timer(Me.components)
Friend WithEvents timerCPS As Timer
Nowhere else references timerCPS in my code and I'm not sure what's wrong
I have replicated your code as given in the question and it is working perfectly fine. It is ticking over every second.
I have modified your code to this:
Public Class FormIdleTimeWaster
Private CPS As Integer = 1
Private Sub timerCPS_Tick(sender As Object, e As EventArgs) Handles timerCPS.Tick
CPS += 1
lblCPS.Text = CStr(CPS)
End Sub
Private Sub FormIdleTimeWaster_Load(sender As Object, e As EventArgs) Handles MyBase.Load
timerCPS.Interval = 1000
timerCPS.Start()
End Sub
End Class
I can clearly see the value in the label increasing by one each tick.
The call to timerCPS.Start() is not needed in timerCPS_Tick.
If you still have an issue then there is some other code you haven't shown.

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".

Working sample of Control.VisibleChanged Event in vb.net

I'm struggling to make the MSDN code sample for the Control.VisibleChanged event work: I don't see the MsgBox.
Private Sub Button_HideLabel(ByVal sender As Object, ByVal e As EventArgs)
myLabel.Visible = False
End Sub 'Button_HideLabel
Private Sub AddVisibleChangedEventHandler()
AddHandler myLabel.VisibleChanged, AddressOf Label_VisibleChanged
End Sub 'AddVisibleChangedEventHandler
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
You need to "wire up" the events to the event handlers.
To start with, to get the code in HideLabel_Click to be called you need it to respond to a click on the button named "HideLabel".
There are two ways to do that: you can use AddHandler or the Handles clause.
To demonstrate the latter:
Option Strict On
Public Class Form1
Private Sub HideLabel_Click(sender As Object, e As EventArgs) Handles HideLabel.Click
myLabel.Visible = False
End Sub
Private Sub myLabel_VisibleChanged(sender As Object, e As EventArgs) Handles myLabel.VisibleChanged
MessageBox.Show("Visible change event raised!!!")
End Sub
End Class
However, you will notice that the message is shown even before the form appears. That is because of what goes on behind the scenes to create the form.
To avoid that happening, you can add the handler after the form has been shown:
Option Strict On
Public Class Form1
Private Sub HideLabel_Click(sender As Object, e As EventArgs) Handles HideLabel.Click
myLabel.Visible = False
End Sub
Private Sub myLabel_VisibleChanged(sender As Object, e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
AddHandler myLabel.VisibleChanged, AddressOf myLabel_VisibleChanged
End Sub
End Class
Another way, in VB2015 and later, is to use a "lambda expression" instead of a separate method, although then you cannot disassociate the handler from the event with RemoveHandler:
Option Strict On
Public Class Form1
Private Sub HideLabel_Click(sender As Object, e As EventArgs) Handles HideLabel.Click
myLabel.Visible = False
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
AddHandler myLabel.VisibleChanged, Sub() MessageBox.Show("Visible change event raised!!!")
End Sub
End Class
Craig was kind enough to [and I quote verbatim] call attention to the importance of Option Strict when you add handlers manually using AddHandler. Without it, the "relaxed delegate convention" may allow adding handlers which don't exactly match the event signature that you won't be able to remove later.
Having said that, Option Strict On isn't a complete safeguard: notice how my last example compiles and works even with the wrong method signature for the handler.
[I suspect that the MSDN code sample was first created in C# as part of a larger example, so some parts have been lost in the translation and excerption.]
I get this is old but came across this post when looking for more information on VisibleChanged and couldn't help but notice that the accept answer may be misleading. If you are using a designer to create your Form and place objects on it, then the accepted answer will be fine. In fact you can get rid of the addHandler because the designer handles that for you. All you would need to do is use a handles clause with your label.
Private Sub Button_HideLabel(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
myLabel.Visible = False
End Sub 'Button_HideLabel
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myLabel.VisibleChanged
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
Where the issue lies with the accepted answer is if you arn't using a designer. Adding handle clauses to "wire up" simply won't work (we can make it work and if anyone is interested in that I'll be happy to post a code snippet of that, but it's not how the accepted answer lays it out). In your case all you need to do is call AddVisibleChangedEventHandler() to set up the handler. that's it. you could have done this by calling it in MyBase.Load
Private Sub Load_Form(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
AddVisibleChangedEventHandler()
End Sub
Private Sub Button_HideLabel(ByVal sender As Object, ByVal e As EventArgs)
myLabel.Visible = False
End Sub 'Button_HideLabel
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
Private Sub AddVisibleChangedEventHandler()
AddHandler myLabel.VisibleChanged, AddressOf Label_VisibleChanged
End Sub
Once again I know this is dated but couldn't help but notice that (more or less assuming) that you are trying to get a msgBox to appear when you click a label. That is you click a label and then toggled the visibility of another label. The other label is the one where the event handler is on for visibility change. So that inevitably gets called when clicking the original label. IF you only want this msgBox to appear when clicking that label and not when the form loads as well, you should change the addHandler statement so that you are adding a handler on the click event.
Private Sub Load_Form(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
AddVisibleChangedEventHandler()
End Sub
Private Sub Label_VisibleChanged(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Visible change event raised!!!")
End Sub 'Label_VisibleChanged
Private Sub AddVisibleChangedEventHandler()
AddHandler otherLabel.Click, AddressOf Label_VisibleChanged
End Sub 'AddVisibleChangedEventHandler
Also Option Strict On has nothing to do with addhandler (From my understanding, could be wrong. please enlighten me if that is the case). Option Strict On is only checking to see that you arn't implicitly typecasting. So for example:
Dim a As Double
Dim b As Integer
a = 10
b = a
results in an error when Option Strict is On but is totally legal if it is off. So in the case of you leaving off the handles clause, you'll never be implicitly typecasting and therefore is not needed.
Hope this helps anyone who sees this question

VB Stopwatch Controlled by Mouse

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

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#.