我有一个带有开始按钮的表单(允许用户根据需要反复运行进程),并且我想在表单加载时发送btnStart.Click
事件,以便进程启动自动。
我为btnStart.Click
事件提供了以下函数,但是如何实际告诉Visual Basic'假设有人点击了按钮并触发了此事件'?
我尝试过非常简单,基本上可行。但是,Visual Studio给了我一个警告Variable 'sender' is used before it has been assigned a value
,所以我猜这不是真正的方法:
Dim sender As Object
btnStart_Click(sender, New EventArgs())
我也尝试使用RaiseEvent btnStart.Click
,但这会产生以下错误:
'btnStart'不是'MyProject.MyFormClass
的事件
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
答案 0 :(得分:23)
您应该将sender
按钮发送到事件处理程序:
btnStart_Click(btnStart, New EventArgs())
答案 1 :(得分:9)
参与筹集活动的步骤如下:
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
所以在你的情况下,它应该如下,
btnStart_Click(btnStart, EventArgs.Empty)
答案 2 :(得分:6)
您正在尝试实施错误的想法。实际上,你必须制作一个子程序来完成这些任务。
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
然后,如果您想呼叫btnStart's click event
,请拨打SeparateSubroutine
。在您的情况下,这应该是正确的方法。
答案 3 :(得分:5)
致电
btnStart.PerformClick()
答案 4 :(得分:0)
您可以将按钮子类化并使其 OnClick
方法公开 as I described here。