如何在 VB.NET 代码中触发事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15831599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:48:07  来源:igfitidea点击:

How do I fire an event in VB.NET code?

vb.neteventsraiseevent

提问by David Gard

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.Clickevent when the form loads, so that the processes start automatically.

我有一个带有开始按钮的表单(允许用户根据需要反复运行进程),并且我想btnStart.Click在表单加载时发送一个事件,以便进程自动启动。

I have the following function for the btnStart.Clickevent, but how do I actually tell Visual Basic 'Pretend someone has clicked the button and fire this event'?

我有以下btnStart.Click事件函数,但我如何实际告诉 Visual Basic '假装有人单击了按钮并触发此事件'?

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:

我试过非常简单,这基本上有效。但是,Visual Studio 给了我一个警告Variable 'sender' is used before it has been assigned a value,所以我猜这不是真正的方法:

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

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

我也试过使用RaiseEvent btnStart.Click,但会出现以下错误:

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

“btnStart”不是“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

回答by MarcinJuraszek

You should send a button as senderinto the event handler:

您应该将按钮发送sender到事件处理程序中:

btnStart_Click(btnStart, New EventArgs())

回答by Dev

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)

回答by Rajaprabhu Aravindasamy

You are trying to implement a badidea. Actually, you have to make a subroutineto 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.

然后无论您想在何处调用btnStart's click event,只需调用那个SeparateSubroutine。在您的情况下,这应该是正确的方法。

回答by Mandeep Singh

Just Call

就打电话

btnStart.PerformClick()