David Gard David Gard - 1 year ago 68
Vb.net Question

How do I fire an Event in VB.Net code?

I have a form that has a start button (to allow users to run the processes over and over if they wish), and I want to send a

event when the form loads, so that the processes start automatically.

I have the following function for the
event, but how do I actually tell VB 'Pretend someone has clicked the button and fire this event'?

I've tried going very simple, which essentially works. However, Visual Studio gives me a warning
Variable 'sender' is used before it has been assigned a value
, so I'm guessing this is not really the way to do it -

Dim sender As Object
btnStart_Click(sender, New EventArgs())

I have also tried using
RaiseEvent btnStart.Click
, but that gives the following error -

'btnStart' is not an event of 'MyProject.MyFormClass

Any tips on how I should be achieving this would be appreciated. Thanks.

Imports System.ComponentModel

Partial Public Class frmProgress

Private bw As BackgroundWorker = New BackgroundWorker

Public Sub New()


' Set up the BackgroundWorker
bw.WorkerReportsProgress = True
bw.WorkerSupportsCancellation = True
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted

' Fire the 'btnStart.click' event when the form loads
Dim sender As Object
btnStart_Click(sender, New EventArgs())

End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click

If Not bw.IsBusy = True Then

' Enable the 'More >>' button on the form, as there will now be details for users to view
Me.btnMore.Enabled = True

' Update the form control settings so that they correctly formatted when the processing starts


End If

End Sub

' Other functions exist here

End Class

Answer Source

You should send button as sender into event handler:

btnStart_Click(btnStart, New EventArgs())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download