18

開始ボタンのあるフォームがあり (ユーザーが必要に応じてプロセスを何度も実行できるようにするため)、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
4

5 に答える 5

26

ボタンをsenderイベント ハンドラーに送信する必要があります。

btnStart_Click(btnStart, New EventArgs())
于 2013-04-05T10:36:55.993 に答える
10

イベントの発生に関与する手順は次のとおりです。

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)
于 2013-04-05T10:50:06.270 に答える
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。あなたの場合、これは正しい方法です。

于 2013-04-05T11:03:14.437 に答える