How do I fire an event in VB.NET code?
59737 просмотра
4 ответа
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 btnStart.Click
event when the form loads, so that the processes start automatically.
I have the following function for the btnStart.Click
event, but how do I actually tell Visual Basic '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
Code
Imports System.ComponentModel
Partial Public Class frmProgress
Private bw As BackgroundWorker = New BackgroundWorker
Public Sub New()
InitializeComponent()
' 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
set_form_on_start()
bw.RunWorkerAsync()
End If
End Sub
' Other functions exist here
End Class
Автор: David Gard
Источник
Размещён: 12.11.2019 09:53
Ответы (4)
23 плюса
You should send a button as sender
into the event handler:
btnStart_Click(btnStart, New EventArgs())
Автор: MarcinJuraszek
Размещён: 05.04.2013 10:36
9 плюса
Steps in involved in raising an event is as follows,
Public Event ForceManualStep As EventHandler
RaiseEvent ForceManualStep(Me, EventArgs.Empty)
AddHandler ForceManualStep, AddressOf ManualStepCompletion
Private Sub ManualStepCompletion(sender As Object, e As EventArgs)
End Sub
So in your case, it should be as below,
btnStart_Click(btnStart, EventArgs.Empty)
Автор: Dev
Размещён: 05.04.2013 10:50
6 плюса
You are trying to implement a bad idea. Actually, you have to make a subroutine to accomplish these kind of tasks.
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
call SeparateSubroutine()
End Sub
private sub SeparateSubroutine()
'Your code here.
End Sub
And then whereever you want to call the btnStart's click event
, just call that SeparateSubroutine
. This should be a correct way in your case.
5 плюса
Вопросы из категории :
- vb.net Setting Objects to Null/Nothing after use in .NET
- vb.net Случайное целое число в VB.NET
- vb.net Лучший способ в asp.net заставить https для всего сайта?
- vb.net Кто-нибудь знает библиотеку для работы с парами количество / единица измерения?
- vb.net Одиночная форма Скрыть при запуске
- vb.net Что такое CD-версия InputDialog от VB.net?
- events Каковы различия между делегатами и событиями?
- events Вызывать события C # извне класса-владельца?
- events Как мне обработать событие закрытия окна в Tkinter?
- events Зачем использовать EventArgs.Empty вместо null?
- events Event binding on dynamically created elements?
- events Вызывать события C # с помощью метода расширения - это плохо?
- raiseevent Событие DragDrop не возбуждено
- raiseevent How do I fire an event in VB.NET code?
- raiseevent Как «AddHandler» пытается отловить событие из внешней библиотеки
- raiseevent Подфункции против событий, в чем разница?
- raiseevent Как поднять событие из пользовательского контроля в другую форму vb.net
- raiseevent VB.NET вызывает обработчики событий по ссылке