gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:18:19 27 4
gpt4 key购买 nike

我有一个带有开始按钮的表单(允许用户根据需要反复运行进程),并且我想在加载表单时发送一个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

最佳答案

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

btnStart_Click(btnStart, New EventArgs())

关于vb.net - 如何在VB.NET代码中触发事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15831599/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com