VB.Net - Call a Sub with events

前端 未结 3 1796
北恋
北恋 2021-01-27 05:35

Is it possible to Call a Sub with events?

I have the following sub, and when I press a button, I will trigger a Msgbox.

Public Class SelectLevel

    Pub         


        
相关标签:
3条回答
  • 2021-01-27 05:44

    Your code is correct except in one place. While calling, you have to give the required parameters

    Your Sub Procedure is : Public Shared Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click

    But you're calling as :Call SelectLevel.Button_Start_Click (without any values to be sent)

    Call like this:

    Private Sub Testing()
        SelectLevel.Button_Start_Click(Button_Start, Nothing) ' I tried this aswell but didn't work.
    End Sub
    

    This will do your work

    0 讨论(0)
  • 2021-01-27 05:49

    If the event you need is only click event then there is an easy way PerformClick function

    Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Button1.PerformClick()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox("pressed")
    End Sub 
    End Class
    
    0 讨论(0)
  • 2021-01-27 05:54

    You need to pass the parameters too.

    This should work:

    Public Class TestClass
    
        Private Sub Testing
            Call SelectLevel.Button_Start_Click(Nothing, System.EventArgs.Empty)
        End Sub
    
    End Class
    
    0 讨论(0)
提交回复
热议问题